2006-11-24 06:39:15 +00:00
|
|
|
<?php
|
|
|
|
require_once ($CFG->libdir.'/formslib.php');
|
|
|
|
/**
|
|
|
|
* This class adds extra methods to form wrapper specific to be used for module
|
|
|
|
* add / update forms (mod/{modname}.mod_form.php replaces deprecared mod/{modname}/mod.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class moodleform_mod extends moodleform {
|
|
|
|
/**
|
|
|
|
* Instance of the module that is being updated. This is the id of the {prefix}{modulename}
|
|
|
|
* record. Can be used in form definition. Will be "" if this is an 'add' form and not an
|
|
|
|
* update one.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
var $_instance;
|
|
|
|
/**
|
|
|
|
* Section of course that module instance will be put in or is in.
|
|
|
|
* This is always the section number itself.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
var $_section;
|
|
|
|
/**
|
|
|
|
* Coursemodle record of the module that is being updated. Will be null if this is an 'add' form and not an
|
|
|
|
* update one.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
var $_cm;
|
|
|
|
|
|
|
|
function moodleform_mod($instance, $section, $cm) {
|
|
|
|
$this->_instance = $instance;
|
|
|
|
$this->_section = $section;
|
|
|
|
$this->_cm = $cm;
|
|
|
|
parent::moodleform('modedit.php');
|
|
|
|
}
|
2007-07-31 14:34:11 +00:00
|
|
|
|
2006-11-24 06:39:15 +00:00
|
|
|
/**
|
2007-01-16 15:36:33 +00:00
|
|
|
* Only available on moodleform_mod.
|
2006-11-24 06:39:15 +00:00
|
|
|
*
|
|
|
|
* @param array $default_values passed by reference
|
|
|
|
*/
|
2007-01-16 15:36:33 +00:00
|
|
|
function data_preprocessing(&$default_values){
|
2006-11-24 06:39:15 +00:00
|
|
|
}
|
2007-07-31 14:34:11 +00:00
|
|
|
|
|
|
|
function definition_after_data() {
|
|
|
|
global $COURSE;
|
|
|
|
$mform =& $this->_form;
|
|
|
|
|
|
|
|
if ($id = $mform->getElementValue('update')) {
|
|
|
|
$modulename = $mform->getElementValue('modulename');
|
|
|
|
$instance = $mform->getElementValue('instance');
|
|
|
|
|
|
|
|
if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
|
|
|
|
'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if (!empty($item->outcomeid)) {
|
|
|
|
$elname = 'outcome_'.$item->outcomeid;
|
|
|
|
if ($mform->elementExists($elname)) {
|
|
|
|
$mform->hardFreeze($elname); // prevent removing of existing outcomes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-02 10:38:18 +00:00
|
|
|
// form verification
|
|
|
|
function validation($data) {
|
|
|
|
global $COURSE;
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
$grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$data['modulename'],
|
|
|
|
'iteminstance'=>$data['instance'], 'itemnumber'=>0, 'courseid'=>$COURSE->id));
|
|
|
|
if ($data['coursemodule']) {
|
|
|
|
$cm = get_record('course_modules', 'id', $data['coursemodule']);
|
|
|
|
} else {
|
|
|
|
$cm = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify the idnumber
|
|
|
|
if (!grade_verify_idnumber($data['cmidnumber'], $grade_item, $cm)) {
|
|
|
|
$errors['cmidnumber'] = get_string('idnumbertaken');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($errors) == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-24 06:39:15 +00:00
|
|
|
/**
|
|
|
|
* Load in existing data as form defaults. Usually new entry defaults are stored directly in
|
|
|
|
* form definition (new entry form); this function is used to load in data where values
|
|
|
|
* already exist and data is being edited (edit entry form).
|
|
|
|
*
|
|
|
|
* @param mixed $default_values object or array of default values
|
|
|
|
*/
|
2007-01-12 19:01:31 +00:00
|
|
|
function set_data($default_values) {
|
2006-11-24 06:39:15 +00:00
|
|
|
if (is_object($default_values)) {
|
|
|
|
$default_values = (array)$default_values;
|
|
|
|
}
|
2007-01-16 18:01:02 +00:00
|
|
|
$this->data_preprocessing($default_values);
|
2007-01-12 19:01:31 +00:00
|
|
|
parent::set_data($default_values + $this->standard_coursemodule_elements_settings());//never slashed for moodleform_mod
|
2006-11-24 06:39:15 +00:00
|
|
|
}
|
2007-07-31 14:34:11 +00:00
|
|
|
|
2006-11-24 06:39:15 +00:00
|
|
|
/**
|
|
|
|
* Adds all the standard elements to a form to edit the settings for an activity module.
|
|
|
|
*
|
|
|
|
* @param bool $supportsgroups does this module support groups?
|
|
|
|
*/
|
|
|
|
function standard_coursemodule_elements($supportsgroups=true){
|
2007-08-02 08:32:49 +00:00
|
|
|
global $COURSE, $CFG;
|
2006-11-24 06:39:15 +00:00
|
|
|
$mform =& $this->_form;
|
2007-08-01 04:40:56 +00:00
|
|
|
|
|
|
|
if (!empty($CFG->enableoutcomes)) {
|
|
|
|
if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
|
|
|
|
$mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
|
|
|
|
foreach($outcomes as $outcome) {
|
|
|
|
$mform->addElement('advcheckbox', 'outcome_'.$outcome->id, $outcome->get_name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-13 14:50:22 +00:00
|
|
|
$mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
|
2006-11-24 06:39:15 +00:00
|
|
|
if ($supportsgroups){
|
2007-07-07 14:18:30 +12:00
|
|
|
// TODO: we must define this as mod property!
|
2006-11-24 06:39:15 +00:00
|
|
|
$mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
|
|
|
|
}
|
|
|
|
$mform->addElement('modvisible', 'visible', get_string('visible'));
|
2007-04-21 13:15:34 +00:00
|
|
|
$mform->addElement('text', 'cmidnumber', get_string('idnumber'));
|
2007-07-31 14:34:11 +00:00
|
|
|
|
2006-11-24 06:39:15 +00:00
|
|
|
$this->standard_hidden_coursemodule_elements();
|
|
|
|
}
|
|
|
|
|
|
|
|
function standard_hidden_coursemodule_elements(){
|
|
|
|
$mform =& $this->_form;
|
|
|
|
$mform->addElement('hidden', 'course', 0);
|
|
|
|
$mform->setType('course', PARAM_INT);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'coursemodule', 0);
|
|
|
|
$mform->setType('coursemodule', PARAM_INT);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'section', 0);
|
|
|
|
$mform->setType('section', PARAM_INT);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'module', 0);
|
|
|
|
$mform->setType('module', PARAM_INT);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'modulename', '');
|
|
|
|
$mform->setType('modulename', PARAM_SAFEDIR);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'instance', 0);
|
|
|
|
$mform->setType('instance', PARAM_INT);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'add', 0);
|
|
|
|
$mform->setType('add', PARAM_ALPHA);
|
|
|
|
|
|
|
|
$mform->addElement('hidden', 'update', 0);
|
|
|
|
$mform->setType('update', PARAM_INT);
|
2006-12-04 09:36:30 +00:00
|
|
|
|
|
|
|
$mform->addElement('hidden', 'return', 0);
|
|
|
|
$mform->setType('return', PARAM_BOOL);
|
2006-11-24 06:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is called by course/modedit.php to setup defaults for standard form
|
|
|
|
* elements.
|
|
|
|
*
|
|
|
|
* @param object $course
|
|
|
|
* @param object $cm
|
|
|
|
* @param integer $section
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function standard_coursemodule_elements_settings(){
|
2007-04-21 13:15:34 +00:00
|
|
|
return ($this->modgroupmode_settings() + $this->modvisible_settings());
|
2006-11-24 06:39:15 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This is called from modedit.php to load the default for the groupmode element.
|
|
|
|
*
|
|
|
|
* @param object $course
|
|
|
|
* @param object $cm
|
|
|
|
*/
|
|
|
|
function modgroupmode_settings(){
|
|
|
|
global $COURSE;
|
|
|
|
return array('groupmode'=>groupmode($COURSE, $this->_cm));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This is called from modedit.php to set the default for modvisible form element.
|
|
|
|
*
|
|
|
|
* @param object $course
|
|
|
|
* @param object $cm
|
|
|
|
* @param integer $section section is a db id when updating a activity config
|
|
|
|
* or the section no when adding a new activity
|
|
|
|
*/
|
|
|
|
function modvisible_settings(){
|
|
|
|
global $COURSE;
|
|
|
|
$cm=$this->_cm;
|
|
|
|
$section=$this->_section;
|
|
|
|
if ($cm) {
|
|
|
|
$visible = $cm->visible;
|
|
|
|
} else {
|
|
|
|
$visible = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hiddensection = !get_field('course_sections', 'visible', 'section', $section, 'course', $COURSE->id);
|
|
|
|
if ($hiddensection) {
|
|
|
|
$visible = 0;
|
|
|
|
}
|
|
|
|
return array('visible'=>$visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-08-01 04:40:56 +00:00
|
|
|
?>
|