Some mform refactoring

Uses customdata instead of modified constructor.
This commit is contained in:
David Mudrak
2010-01-04 17:37:53 +00:00
parent 9277982f0f
commit 6d86051250
6 changed files with 38 additions and 49 deletions
+2 -4
View File
@@ -81,13 +81,11 @@ $strategy = new $classname($workshop);
$dimensions = $strategy->load_dimensions();
// load the form to edit the grading strategy dimensions
$mform = $strategy->get_edit_strategy_form($selfurl, true, count($dimensions));
$mform = $strategy->get_edit_strategy_form($selfurl, count($dimensions));
// initialize form data
$formdata = new stdClass;
$formdata->workshopid = $workshop->id;
$formdata->strategy = $workshop->strategy;
$formdata->dimensions = $dimensions;
$formdata->dimensions = $dimensions;
$mform->set_data($formdata);
if ($mform->is_cancelled()) {
@@ -68,7 +68,7 @@ class workshop_edit_accumulative_strategy_form extends workshop_edit_strategy_fo
$numofdimensionstoadd = 2;
$numofinitialdimensions = 3;
$numofdisplaydimensions = max($this->numofdimensions + $numofdimensionstoadd, $numofinitialdimensions);
$numofdisplaydimensions = max($this->nocurrentdims + $numofdimensionstoadd, $numofinitialdimensions);
$this->repeat_elements($repeated, $numofdisplaydimensions, $repeatedoptions, 'numofdimensions', 'adddimensions', $numofdimensionstoadd, get_string('addmoredimensionsaccumulative', 'workshop', $numofdimensionstoadd));
}
@@ -24,9 +24,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page
}
defined('MOODLE_INTERNAL') || die();
require_once(dirname(dirname(__FILE__)) . '/strategy.php'); // parent class
+5 -25
View File
@@ -43,26 +43,8 @@ class workshop_edit_strategy_form extends moodleform {
/** strategy logic instance that this class is editor of */
protected $strategy;
/** number of dimensions that are populated from DB */
protected $numofdimensions;
/**
* Constructor
*
* @param object $strategy The instance of the strategy logic
* @param str $actionurl URL to handle data
* @param bool $editable Should the form be editable?
* @param int $initialdimensions Number of assessment dimensions that are already filled
* @access public
* @return void
*/
public function __construct(workshop_strategy $strategy, $actionurl, $editable=true, $initialdimensions=0) {
$this->strategy = $strategy;
$this->numofdimensions = $initialdimensions;
parent::moodleform($actionurl, null, 'post', '', array('class' => 'editstrategyform'), $editable);
}
/** number of current dimensions loaded from DB */
protected $nocurrentdims;
/**
* Add the fields that are common for all grading strategies.
@@ -79,12 +61,10 @@ class workshop_edit_strategy_form extends moodleform {
global $CFG;
$mform = $this->_form;
$this->strategy = $this->_customdata['strategy'];
$this->nocurrentdims = $this->_customdata['nocurrentdims'];
$mform->addElement('hidden', 'workshopid');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'strategy');
$mform->setType('id', PARAM_ALPHA);
$mform->addElement('hidden', 'strategyname', $this->strategy->name);
$this->definition_inner($mform);
+28 -13
View File
@@ -34,15 +34,16 @@ interface workshop_strategy_interface {
/**
* Factory method returning an instance of an assessment form editor class
*
* The returned class will probably expand the base workshop_edit_strategy_form
* The returned class will probably expand the base workshop_edit_strategy_form. The number of
* dimensions that will be passed by set_data() must be already known here becase the
* definition() of the form has to know the number and it is called before set_data().
*
* @param string $actionurl URL of the action handler script
* @param boolean $edit Open the form in editing mode
* @param int $nodimensions Number of dimensions to be provided by set_data
* @param int $nocurrentdims Number of current dimensions to be set later by set_data()
* @access public
* @return object The instance of the assessment form editor class
*/
public function get_edit_strategy_form($actionurl, $edit=true, $nodimensions=0);
public function get_edit_strategy_form($actionurl, $nocurrentdims=0);
/**
@@ -83,7 +84,7 @@ class workshop_strategy implements workshop_strategy_interface {
public $name;
/** the parent workshop instance */
protected $_workshop;
protected $workshop;
/**
* Constructor
@@ -94,8 +95,8 @@ class workshop_strategy implements workshop_strategy_interface {
*/
public function __construct($workshop) {
$this->name = $workshop->strategy;
$this->_workshop = $workshop;
$this->name = $workshop->strategy;
$this->workshop = $workshop;
}
@@ -105,7 +106,7 @@ class workshop_strategy implements workshop_strategy_interface {
* By default, the class is defined in grading/{strategy}/gradingform.php and is named
* workshop_edit_{strategy}_strategy_form
*/
public function get_edit_strategy_form($actionurl, $edit=true, $nodimensions=0) {
public function get_edit_strategy_form($actionurl, $nocurrentdims=0) {
global $CFG; // needed because the included files use it
$strategyform = dirname(__FILE__) . '/' . $this->name . '/gradingform.php';
@@ -116,7 +117,15 @@ class workshop_strategy implements workshop_strategy_interface {
}
$classname = 'workshop_edit_' . $this->name . '_strategy_form';
return new $classname($this, $actionurl, $edit, $nodimensions);
$customdata = new stdClass;
$customdata = array(
'strategy' => $this,
'nocurrentdims' => $nocurrentdims,
);
$attributes = array('class' => 'editstrategyform');
return new $classname($actionurl, $customdata, 'post', '', $attributes);
}
@@ -133,7 +142,7 @@ class workshop_strategy implements workshop_strategy_interface {
public function load_dimensions() {
global $DB;
return $DB->get_records('workshop_forms_' . $this->name, array('workshopid' => $this->_workshop->id), 'sort');
return $DB->get_records('workshop_forms_' . $this->name, array('workshopid' => $this->workshop->id), 'sort');
}
@@ -153,6 +162,11 @@ class workshop_strategy implements workshop_strategy_interface {
public function save_dimensions($data) {
global $DB;
if (!isset($data->strategyname) || ($data->strategyname != $this->name)) {
// the workshop strategy has changed since the form was opened for editing
throw new moodle_exception('strategyhaschanged', 'workshop');
}
$data = $this->cook_form_data($data);
foreach ($data as $record) {
@@ -173,7 +187,7 @@ class workshop_strategy implements workshop_strategy_interface {
/**
* The default implementation transposes the returned structure
*
* It automatically adds 'sort' column and 'workshopid' column into every record.
* It automatically adds some columns into every record.
* The sorting is done by the order of the returned array and starts with 1.
*
* @param object $raw
@@ -185,8 +199,9 @@ class workshop_strategy implements workshop_strategy_interface {
foreach (array_flip($this->map_dimension_fieldnames()) as $formfield => $dbfield) {
for ($k = 0; $k < $raw->numofdimensions; $k++) {
$cook[$k]->{$dbfield} = isset($raw->{$formfield}[$k]) ? $raw->{$formfield}[$k] : null;
$cook[$k]->sort = $k + 1;
$cook[$k]->workshopid = $raw->workshopid;
$cook[$k]->descriptionformat = FORMAT_HTML;
$cook[$k]->sort = $k + 1;
$cook[$k]->workshopid = $this->workshop->id;
}
}
return $cook;
+1 -3
View File
@@ -24,9 +24,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page
}
defined('MOODLE_INTERNAL')) || die();
// Make sure the code being tested is accessible.
require_once($CFG->dirroot . '/mod/workshop/lib.php'); // Include the code to test