/** * @return bool */ public static function validate($params) { $dataformid = $params['dataformid']; $viewid = $params['viewid']; $df = \mod_dataform_dataform::instance($dataformid); $view = $df->view_manager->get_view_by_id($viewid); // Views manager can access any view in any mode. if (has_capability('mod/dataform:manageviews', $df->context)) { return true; } // Visible/Hidden view. $params['capabilities'] = array('mod/dataform:viewaccess'); if ($view->visible and !parent::validate($params)) { return false; } // Disabled view. $params['capabilities'] = array('mod/dataform:viewaccessdisabled'); if (!$view->visible and !parent::validate($params)) { return false; } // Early access. $params['capabilities'] = array('mod/dataform:viewaccessearly'); if ($df->is_early() and !parent::validate($params)) { return false; } // Late access. $params['capabilities'] = array('mod/dataform:viewaccesslate'); if ($df->is_past_due() and !parent::validate($params)) { return false; } return true; }
public function test_generator() { global $DB; $this->resetAfterTest(); // Reset dataform local cache. \mod_dataform_instance_store::unregister(); $this->setAdminUser(); $generator = $this->getDataGenerator(); // There should be no instances at this point. $instances = array(); $this->assertEquals(count($instances), $DB->count_records('dataform')); // Verify the datafrom generator. $dataformgenerator = $generator->get_plugin_generator('mod_dataform'); $this->assertInstanceOf('mod_dataform_generator', $dataformgenerator); $this->assertEquals('dataform', $dataformgenerator->get_modulename()); // Add course. $course = $generator->create_course(); // Add instances. $dataset = $this->createCsvDataSet(array('cases' => __DIR__ . '/fixtures/tc_generator.csv')); $cases = $dataset->getTable('cases'); $columns = $dataset->getTableMetaData('cases')->getColumns(); for ($r = 0; $r < $cases->getRowCount(); $r++) { $case = array_combine($columns, $cases->getRow($r)); $case['course'] = $course->id; // Create the instance. $data = $dataformgenerator->create_instance($case); $instances[] = $data->id; $this->assertEquals(count($instances), $DB->count_records('dataform')); // Update the instance. $df = \mod_dataform_dataform::instance($data->id); $df->update($df->data); // Verify instances count. $this->assertEquals(count($instances), $DB->count_records('dataform')); // Verify course id. $this->assertEquals($df->course->id, $course->id); // Verify course module. $cm = get_coursemodule_from_instance('dataform', $df->id); $this->assertEquals($df->cm->id, $cm->id); $this->assertEquals($df->id, $cm->instance); $this->assertEquals('dataform', $cm->modname); $this->assertEquals($course->id, $cm->course); // Verify context. $context = context_module::instance($cm->id); $this->assertEquals($df->context->id, $context->id); $this->assertEquals($df->cm->id, $context->instanceid); // Test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE! if ($data->grade) { $gitemparams = array('courseid' => $course->id, 'itemtype' => 'mod', 'itemmodule' => 'dataform', 'iteminstance' => $data->id); $gitem = $DB->get_record('grade_items', $gitemparams); $this->assertNotEmpty($gitem); $this->assertEquals(100, $gitem->grademax); $this->assertEquals(0, $gitem->grademin); $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype); } } $dataid = array_pop($instances); \mod_dataform_dataform::instance($dataid)->delete(); $this->assertEquals(count($instances), $DB->count_records('dataform')); }
/** * Tests the backup and restore of single activity to same course (duplicate) * when it contains fields and views. */ public function test_duplicate() { global $DB, $CFG; $this->resetAfterTest(true); $this->setAdminUser(); $generator = $this->getDataGenerator(); $dataformgenerator = $generator->get_plugin_generator('mod_dataform'); // Create a course. $course = $generator->create_course(); // DATAFORM 1. $params = array('course' => $course->id, 'grade' => 100); $dataform1 = $dataformgenerator->create_instance($params); $df1 = mod_dataform_dataform::instance($dataform1->id); // Add fields. $fieldtypes = array_keys(core_component::get_plugin_list('dataformfield')); $fieldtypescount = count($fieldtypes); foreach ($fieldtypes as $type) { $df1->field_manager->add_field($type); } // Add views. $viewtypes = array_keys(core_component::get_plugin_list('dataformview')); $viewtypescount = count($viewtypes); foreach ($viewtypes as $type) { $df1->view_manager->add_view($type); } // Fetch the grade item. $params = array('itemtype' => 'mod', 'itemmodule' => 'dataform', 'iteminstance' => $dataform1->id, 'courseid' => $course->id, 'itemnumber' => 0); $gradeitem1 = grade_item::fetch($params); // Check number of dataforms. $this->assertEquals(1, $DB->count_records('dataform')); // Check number of fields. $this->assertEquals($fieldtypescount, $DB->count_records('dataform_fields')); $this->assertEquals($fieldtypescount, $DB->count_records('dataform_fields', array('dataid' => $dataform1->id))); // Check number of views. $this->assertEquals($viewtypescount, $DB->count_records('dataform_views')); $this->assertEquals($viewtypescount, $DB->count_records('dataform_views', array('dataid' => $dataform1->id))); // Check number of filters. // $this->assertEquals(2, $DB->count_records('dataform_filters')); // $this->assertEquals(2, $DB->count_records('dataform_filters', array('dataid' => $dataform1->id)));. // DUPLICATE the dataform instance. $dataform2 = $dataformgenerator->duplicate_instance($course, $dataform1->cmid); // Check number of dataforms. $this->assertEquals(2, $DB->count_records('dataform')); // Check duplication of fields. $this->assertEquals($fieldtypescount * 2, $DB->count_records('dataform_fields')); $this->assertEquals($fieldtypescount, $DB->count_records('dataform_fields', array('dataid' => $dataform1->id))); $this->assertEquals($fieldtypescount, $DB->count_records('dataform_fields', array('dataid' => $dataform2->id))); // Check duplication of views. $this->assertEquals($viewtypescount * 2, $DB->count_records('dataform_views')); $this->assertEquals($viewtypescount, $DB->count_records('dataform_views', array('dataid' => $dataform1->id))); $this->assertEquals($viewtypescount, $DB->count_records('dataform_views', array('dataid' => $dataform2->id))); // Check number of filters. // $this->assertEquals(4, $DB->count_records('dataform_filters'); // $this->assertEquals(2, $DB->count_records('dataform_filters', array('dataid' => $dataform1->id)); // $this->assertEquals(2, $DB->count_records('dataform_filters', array('dataid' => $dataform2->id));. // Dataform cleanup. $dataformgenerator->delete_all_instances(); }
/** * @return bool */ public static function validate($params) { $dataformid = $params['dataformid']; $df = \mod_dataform_dataform::instance($dataformid); // Cannot add in a view that does not allow submission. if (!empty($params['viewid'])) { $view = $df->view_manager->get_view_by_id($params['viewid']); if (!$view or !$view->allows_submission()) { return false; } } // User at max entries (per interval). if ($df->user_at_max_entries(true)) { // No more entries for you (come back next interval or so). return false; } // Early entries. if ($df->is_early()) { $params['capabilities'] = array('mod/dataform:entryearlyadd'); if (!parent::validate($params)) { return false; } } // Late entries. if ($df->is_past_due()) { $params['capabilities'] = array('mod/dataform:entrylateadd'); if (!parent::validate($params)) { return false; } } $entry = !empty($params['entry']) ? $params['entry'] : \mod_dataform\pluginbase\dataformentry::blank_instance($df); // Own entry. if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) { $params['capabilities'] = array('mod/dataform:entryownadd'); return parent::validate($params); } // Group entry. if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) { if (groups_is_member($entry->groupid)) { $params['capabilities'] = array('mod/dataform:entrygroupadd'); return parent::validate($params); } } // Anonymous entry. if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) { if ((isguestuser() or !isloggedin()) and $df->anonymous) { return true; } $params['capabilities'] = array('mod/dataform:entryanonymousadd'); return parent::validate($params); } // Any entry. if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) { $params['capabilities'] = array('mod/dataform:entryanyadd'); return parent::validate($params); } return false; }
/** * Test is/not empty criterion. */ public function test_is_not_empty() { global $DB; $generator = $this->getDataGenerator(); $dataformgenerator = $generator->get_plugin_generator('mod_dataform'); $this->setAdminUser(); // Add a dataform. $dataform = $dataformgenerator->create_instance(array('course' => $this->course)); $dataformid = $dataform->id; $df = \mod_dataform_dataform::instance($dataformid); // Add content fields. $fieldtypes = array('text', 'textarea', 'select', 'radiobutton', 'selectmulti', 'checkbox', 'url', 'number', 'time'); $fields = array(); foreach ($fieldtypes as $type) { $fields[$type] = $df->field_manager->add_field($type); } // Add csv view. $importview = $df->view_manager->add_view('csv'); // Import entries. $eaufieldid = dataformfield_entryauthor_entryauthor::INTERNALID; $options = array('settings' => array()); foreach ($fields as $type => $field) { $settings = array('name' => $type); if (in_array($type, array('select', 'radiobutton', 'selectmulti', 'checkbox'))) { $settings['allownew'] = true; } $options['settings'][$field->id] = array('' => $settings); } $content1 = array('text' => 'Some single line text.', 'textarea' => 'First line of multiline text.<br /> Second line of multiline text.', 'select' => 'SL 1', 'radiobutton' => 'RB 1', 'selectmulti' => 'SLM 1', 'checkbox' => 'CB 1', 'url' => 'http://substantialmethods.com', 'number' => '7', 'time' => '22 July 2015 1:10 PM'); $content2 = array('text' => '', 'textarea' => '', 'select' => '', 'radiobutton' => '', 'selectmulti' => '', 'checkbox' => '', 'url' => '', 'number' => '', 'time' => ''); $csvdata = array(implode(',', array_keys($content1)), implode(',', $content1), implode(',', $content2), implode(',', $content2)); $data = new stdClass(); $data->eids = array(); $data->errors = array(); $data = $importview->process_csv($data, implode("\n", $csvdata), $options); $importresult = $importview->execute_import($data); // Get an entry manager for a view. $entryman = $importview->entry_manager; // Search is empty. foreach ($fieldtypes as $type) { $instance = $dataformgenerator->create_filter(array('dataid' => $df->id, 'searchoptions' => "AND,{$type},content,,,")); $filter = new \mod_dataform\pluginbase\dataformfilter($instance); $entryman->set_content(array('filter' => $filter)); $this->assertEquals(3, $entryman->get_count($entryman::COUNT_VIEWABLE)); $this->assertEquals(2, $entryman->get_count($entryman::COUNT_FILTERED)); } // Search not empty. foreach ($fieldtypes as $type) { $instance = $dataformgenerator->create_filter(array('dataid' => $df->id, 'searchoptions' => "AND,{$type},content,NOT,,")); $filter = new \mod_dataform\pluginbase\dataformfilter($instance); $entryman->set_content(array('filter' => $filter)); $this->assertEquals(3, $entryman->get_count($entryman::COUNT_VIEWABLE)); $this->assertEquals(1, $entryman->get_count($entryman::COUNT_FILTERED)); } }
/** * @return bool */ public static function validate($params) { $dataformid = $params['dataformid']; $df = \mod_dataform_dataform::instance($dataformid); // Must have fieldid. if (empty($params['fieldid'])) { return false; } $field = $df->field_manager->get_field_by_id($params['fieldid']); $params['field'] = $field; if (!$field->editable and !has_capability('mod/dataform:manageentries', $field->df->context)) { return false; } // Must have entry. $entry = $params['entry']; // Get blank instance for new entries. if ($entry->id < 0) { $entry = \mod_dataform\pluginbase\dataformentry::blank_instance($df); } // Early access. if ($df->is_early()) { $params['capabilities'] = array('mod/dataform:entryearlyupdate'); if (!parent::validate($params)) { return false; } } // Late access. if ($df->is_past_due()) { $params['capabilities'] = array('mod/dataform:entrylateupdate'); if (!parent::validate($params)) { return false; } } // Own entry. if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) { $params['capabilities'] = array('mod/dataform:entryownupdate'); return parent::validate($params); } // Group entry. if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) { $params['capabilities'] = array('mod/dataform:entrygroupupdate'); return parent::validate($params); } // Anonymous entry. if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) { $params['capabilities'] = array('mod/dataform:entryanonymousupdate'); return parent::validate($params); } // Any entry. if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) { $params['capabilities'] = array('mod/dataform:entryanyupdate'); return parent::validate($params); } return false; }
/** * @return bool */ public static function validate($params) { $dataformid = $params['dataformid']; $df = \mod_dataform_dataform::instance($dataformid); // Cannot update in a view that does not allow submission. if (!empty($params['viewid'])) { $view = $df->view_manager->get_view_by_id($params['viewid']); if (!$view or !$view->allows_submission()) { return false; } } // Unspecified entry // if (empty($params['entry'])) { // return self::has_capability('mod/dataform:entryanyupdate', $params); // }. // Early entries. if ($df->is_early()) { $params['capabilities'] = array('mod/dataform:entryearlyupdate'); if (!parent::validate($params)) { return false; } } // Late entries. if ($df->is_past_due()) { $params['capabilities'] = array('mod/dataform:entrylateupdate'); if (!parent::validate($params)) { return false; } } $entry = !empty($params['entry']) ? $params['entry'] : \mod_dataform\pluginbase\dataformentry::blank_instance($df); // Own entry. if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) { $params['capabilities'] = array('mod/dataform:entryownupdate'); return parent::validate($params); } // Group entry. if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) { if (groups_is_member($entry->groupid)) { $params['capabilities'] = array('mod/dataform:entrygroupupdate'); return parent::validate($params); } } // Anonymous entry. if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) { $params['capabilities'] = array('mod/dataform:entryanonymousupdate'); return parent::validate($params); } // Any entry. if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) { $params['capabilities'] = array('mod/dataform:entryanyupdate'); return parent::validate($params); } return false; }
/** * Sets up a dataform activity in a course. * * @return mod_dataform_dataform */ protected function get_a_dataform($dataformid = null) { $this->setAdminUser(); // The generator used to create a data module. $generator = $this->getDataGenerator()->get_plugin_generator('mod_dataform'); if (!$dataformid) { // Create a dataform instance. $data = $generator->create_instance(array('course' => $this->_course)); $dataformid = $data->id; } return mod_dataform_dataform::instance($dataformid); }
public function validation($data, $files) { if (!($errors = parent::validation($data, $files))) { $filter = $this->_filter; $df = \mod_dataform_dataform::instance($filter->dataid); // Validate unique name. if (empty($data['name']) or $df->name_exists('filters', $data['name'], $filter->id)) { $errors['name'] = get_string('invalidname', 'dataform', get_string('filter', 'dataform')); } } return $errors; }
/** * Test filter events. */ public function test_filter() { global $DB; $generator = $this->getDataGenerator(); $dataformgenerator = $generator->get_plugin_generator('mod_dataform'); $this->setAdminUser(); // Add a dataform. $dataform = $dataformgenerator->create_instance(array('course' => $this->course)); $dataformid = $dataform->id; $df = \mod_dataform_dataform::instance($dataformid); // Add entries. $student1entry = array('dataid' => $dataformid, 'userid' => $this->student1->id); $student2entry = array('dataid' => $dataformid, 'userid' => $this->student2->id); $dataformgenerator->create_entry($student1entry); $dataformgenerator->create_entry($student1entry); $dataformgenerator->create_entry($student1entry); $dataformgenerator->create_entry($student2entry); $dataformgenerator->create_entry($student2entry); // Get an entry manager for a view. $view = $df->view_manager->add_view('aligned'); $entryman = $view->entry_manager; // Default filter (= no filter). $instance = $dataformgenerator->create_filter(array('dataid' => $df->id)); $filter = new \mod_dataform\pluginbase\dataformfilter($instance); $entryman->set_content(array('filter' => $filter)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_VIEWABLE)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_FILTERED)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_DISPLAYED)); $entries = $entryman->entries; $firstentry = reset($entries); $this->assertEquals('student1', $firstentry->username); // Perpage. $instance = $dataformgenerator->create_filter(array('dataid' => $df->id, 'perpage' => 2)); $filter = new \mod_dataform\pluginbase\dataformfilter($instance); $entryman->set_content(array('filter' => $filter)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_VIEWABLE)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_FILTERED)); $this->assertEquals(2, $entryman->get_count($entryman::COUNT_DISPLAYED)); $entries = $entryman->entries; $firstentry = reset($entries); $this->assertEquals('student1', $firstentry->username); // Custom sort. $instance = $dataformgenerator->create_filter(array('dataid' => $df->id, 'sortoptions' => 'EAU,username,1')); $filter = new \mod_dataform\pluginbase\dataformfilter($instance); $entryman->set_content(array('filter' => $filter)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_VIEWABLE)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_FILTERED)); $this->assertEquals(5, $entryman->get_count($entryman::COUNT_DISPLAYED)); $entries = $entryman->entries; $firstentry = reset($entries); $this->assertEquals('student2', $firstentry->username); }
/** * @return bool */ public static function validate($params) { $dataformid = $params['dataformid']; $df = \mod_dataform_dataform::instance($dataformid); // Unspecified entry. if (empty($params['entry'])) { return self::has_capability('mod/dataform:entryanydelete', $params); } // Early entries. if ($df->is_early()) { $params['capabilities'] = array('mod/dataform:entryearlydelete'); if (!parent::validate($params)) { return false; } } // Late entries. if ($df->is_past_due()) { $params['capabilities'] = array('mod/dataform:entrylatedelete'); if (!parent::validate($params)) { return false; } } $entry = !empty($params['entry']) ? $params['entry'] : \mod_dataform\pluginbase\dataformentry::blank_instance($df); // Own entry. if (\mod_dataform\pluginbase\dataformentry::is_own($entry)) { $params['capabilities'] = array('mod/dataform:entryowndelete'); return parent::validate($params); } // Group entry. if (\mod_dataform\pluginbase\dataformentry::is_grouped($entry)) { if (groups_is_member($entry->groupid)) { $params['capabilities'] = array('mod/dataform:entrygroupdelete'); return parent::validate($params); } } // Anonymous entry. if (\mod_dataform\pluginbase\dataformentry::is_anonymous($entry)) { $params['capabilities'] = array('mod/dataform:entryanonymousdelete'); return parent::validate($params); } // Any entry. if (\mod_dataform\pluginbase\dataformentry::is_others($entry)) { $params['capabilities'] = array('mod/dataform:entryanydelete'); return parent::validate($params); } return false; }
/** * Updates activity completion status. * * @return void */ public static function update_require_specific_grade(\core\event\base $event) { global $DB; $entryuserid = $event->relateduserid; $giid = $event->other['itemid']; if (!($gitem = \grade_item::fetch(array('id' => $giid, 'itemmodule' => 'dataform')))) { return; } $dataformid = $gitem->iteminstance; $df = \mod_dataform_dataform::instance($dataformid); // Currently only completion by require entries. if ($df->completionspecificgrade) { $completion = new \completion_info($df->course); if ($completion->is_enabled($df->cm) != COMPLETION_TRACKING_AUTOMATIC) { return; } $completion->update_state($df->cm, COMPLETION_UNKNOWN, $entryuserid); } }
public function definition_general() { $mform =& $this->_form; $filter = $this->_filter; $name = empty($filter->name) ? get_string('filternew', 'dataform') : $filter->name; $description = empty($filter->description) ? '' : $filter->description; $visible = !isset($filter->visible) ? 1 : $filter->visible; $mform->addElement('header', 'general', get_string('general', 'form')); // Name. $mform->addElement('text', 'name', get_string('name')); $mform->setType('name', PARAM_TEXT); $mform->setDefault('name', $name); // Description. $mform->addElement('textarea', 'description', get_string('description')); $mform->setType('description', PARAM_TEXT); $mform->setDefault('description', $description); // Visibility. $mform->addElement('selectyesno', 'visible', get_string('visible')); $mform->setDefault('visible', 1); // Entry type. $menu = array('' => get_string('choosedots')); $df = \mod_dataform_dataform::instance($filter->dataid); if ($entrytypes = $df->entrytypes) { $types = array_map('trim', explode(',', $entrytypes)); $menu = array_merge($menu, array_combine($types, $types)); } $mform->addElement('select', 'entrytype', get_string('entrytype', 'dataform'), $menu); $mform->addHelpButton('entrytype', 'entrytype', 'dataform'); $mform->setDefault('entrytype', $filter->entrytype); // Entries per page. $mform->addElement('text', 'perpage', get_string('viewperpage', 'dataform')); $mform->setType('perpage', PARAM_INT); $mform->addRule('perpage', null, 'numeric', null, 'client'); $mform->setDefault('perpage', $filter->perpage); // Selection method // $options = array(0 => get_string('filterbypage', 'dataform'), 1 => get_string('random', 'dataform')); // $mform->addElement('select', 'selection', get_string('filterselection', 'dataform'), $options); // $mform->setDefault('selection', $filter->selection); // $mform->disabledIf('selection', 'perpage', 'eq', '0');. // Group by // $mform->addElement('select', 'groupby', get_string('filtergroupby', 'dataform'), $fieldoptions); // $mform->setDefault('groupby', $filter->groupby);. }
function xmldb_dataformfield_ratingmdl_upgrade($oldversion) { global $DB; $dbman = $DB->get_manager(); $newversion = 2014111000; if ($oldversion < $newversion) { // Replace field patterns. if ($dataforms = $DB->get_records('dataform')) { foreach (array_keys($dataforms) as $dataformid) { // Get field names of ratingmdl fields. $params = array('dataid' => $dataformid, 'type' => 'ratingmdl'); if (!($fieldnames = $DB->get_records_menu('dataform_fields', $params, '', 'id,name'))) { continue; } // Must have views to continue. if (!$DB->record_exists('dataform_views', array('dataid' => $dataformid))) { continue; } $df = mod_dataform_dataform::instance($dataformid); $replacements = array(); foreach ($fieldnames as $fieldname) { $replacements["[[{$fieldname}:viewurl]]"] = "[[{$fieldname}:view:url]]"; $replacements["[[{$fieldname}:viewinline]]"] = "[[{$fieldname}:view:inline]]"; $replacements["[[{$fieldname}:avg]]"] = "[[{$fieldname}:view:avg]]"; $replacements["[[{$fieldname}:count]]"] = "[[{$fieldname}:view:count]]"; $replacements["[[{$fieldname}:max]]"] = "[[{$fieldname}:view:max]]"; $replacements["[[{$fieldname}:min]]"] = "[[{$fieldname}:view:min]]"; $replacements["[[{$fieldname}:sum]]"] = "[[{$fieldname}:view:sum]]"; } $df->view_manager->replace_patterns_in_views(array_keys($replacements), $replacements); } } upgrade_plugin_savepoint(true, $newversion, 'dataformfield', 'ratingmdl'); } return true; }
/** * Run the tool. * * @return view */ public static function run() { global $DB, $OUTPUT; $summary = optional_param('summary', 0, PARAM_INT); $analyse = optional_param('analyse', 0, PARAM_INT); $confirm = optional_param('confirm', 0, PARAM_INT); $execute = optional_param('execute', 0, PARAM_INT); $baseurl = self::get_url(); $summarylink = \html_writer::link(new \moodle_url($baseurl, array('summary' => 1)), 'Summary'); $analyselink = \html_writer::link(new \moodle_url($baseurl, array('analyse' => 1)), 'Analyse'); $executelink = \html_writer::link(new \moodle_url($baseurl, array('execute' => 1)), 'Execute'); echo \html_writer::tag('div', "{$summarylink} | {$analyselink} | {$executelink}"); if (!($dataforms = $DB->get_records('dataform'))) { return get_string('dataformnone', 'dataform'); } if (!$analyse and !$execute) { return self::get_summary($dataforms); } $brokenpatterns = array(); foreach ($dataforms as $dataformid => $dataform) { $df = \mod_dataform_dataform::instance($dataformid); if (!($views = $df->view_manager->get_views())) { continue; } foreach ($views as $viewid => $view) { if ($updates = $view->patterns_check()) { $brokenpatterns = array_merge($brokenpatterns, $updates); } } } // No problems. if (!$brokenpatterns) { return $OUTPUT->notification(get_string('patternsnonebroken', 'dataform'), 'notifysuccess'); } $table = new \html_table(); $table->head = array('Pattern Name', 'Dataform', 'View', 'Type', 'Problem'); foreach ($brokenpatterns as $info) { $table->data[] = array($info['pattern'], $info['dataform'], $info['view'], $info['type'], $info['problem']); } return \html_writer::table($table); }
/** * Displays patterns clean up form and processes form submission. * * @param int $dataformid The id of the dataform to clean up. Default -1 for all dataforms. * @param int $viewid The id of the view to clean up. Default -1 for all views. * @return void */ public static function patterns_cleanup($dataformid = -1, $viewid = -1) { global $DB, $PAGE, $CFG; if (!$dataformid or !$viewid) { redirect($PAGE->url); } require_once "{$CFG->dirroot}/mod/dataform/view/patternsform.php"; // Get the Dataforms. if ($dataformid == -1) { if (!($dataformids = $DB->get_records('dataform', array(), '', 'id'))) { return false; } $dataformids = array_keys($dataformids); } else { $dataformids = array($dataformid); } // Collate the broken patterns. $brokenpatterns = array(); foreach ($dataformids as $dfid) { $df = mod_dataform_dataform::instance($dfid); // Get the views. if ($viewid == -1) { if (!($views = $df->view_manager->get_views())) { continue; } } else { if (!($view = $df->view_manager->get_view_by_id($viewid))) { continue; } $views = array($view); } foreach ($views as $view) { if ($updates = $view->patterns_check()) { $brokenpatterns = array_merge($brokenpatterns, $updates); } } } if (!$brokenpatterns) { redirect($PAGE->url); } // Get the form. $mform = new mod_dataform_view_patternsform(null, array('patterns' => $brokenpatterns)); // Cancelled. if ($mform->is_cancelled()) { redirect($PAGE->url); } // Clean up. if ($data = $mform->get_data()) { if (!empty($data->replacements)) { $patterns = array_keys($data->replacements); $replacements = $data->replacements; if ($viewid and $viewid != -1) { $df = mod_dataform_dataform::instance($dataformid); $view = $df->view_manager->get_view_by_id($viewid); $view->replace_patterns_in_view($patterns, $replacements); } else { foreach ($dataformids as $dfid) { $df = mod_dataform_dataform::instance($dfid); $df->view_manager->replace_patterns_in_views($patterns, $replacements); } } } // Redirect to refresh the form. $url = new moodle_url($PAGE->url, array('patternscleanup' => $viewid)); redirect($url); } // Heading. echo html_writer::tag('h3', get_string('patternsreplacement', 'dataform')); // Display the form. $actionurl = new moodle_url($PAGE->url, array('patternscleanup' => $viewid)); $customdata = array('patterns' => $brokenpatterns); $mform = new mod_dataform_view_patternsform($actionurl, $customdata); $mform->display(); }
/** * Validates a submitted rating * @param array $params submitted data * context => object the context in which the rated items exists [required] * ratingarea => string 'entry' or 'activity' [required] * itemid => int the ID of the object being rated * scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required] * rating => int the submitted rating * rateduserid => int the id of the user whose items have been rated. NOT the user who submitted the ratings. 0 to update all. [required] * aggregation => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required] * @return boolean true if the rating is valid. Will throw rating_exception if not */ function dataform_rating_validate($params) { $ratingarea = $params['ratingarea']; $itemid = $params['itemid']; $context = $params['context']; $df = mod_dataform_dataform::instance(0, $context->instanceid); $rating = $df->field_manager->get_field_by_name($ratingarea); return $rating->validation($params); }
/** * */ public function delete_presets($presetids) { if (!($pids = explode(',', $presetids))) { return false; } $fs = get_file_storage(); // Try first course area // Must be in Dataform. if ($this->_dataformid) { $df = mod_dataform_dataform::instance($this->_dataformid); if (!has_capability('mod/dataform:managepresets', $df->context)) { return false; } $coursecontext = context_course::instance($df->course->id); $contextid = $coursecontext->id; if ($files = $fs->get_area_files($contextid, 'mod_dataform', self::PRESET_COURSEAREA)) { foreach ($files as $file) { if (empty($pids)) { break; } if (!$file->is_directory()) { $key = array_search($file->get_id(), $pids); if ($key !== false) { $file->delete(); unset($pids[$key]); } } } } } // Try site area. if (!empty($pids)) { if ($files = $fs->get_area_files(SYSCONTEXTID, 'mod_dataform', self::PRESET_SITEAREA)) { foreach ($files as $file) { if (empty($pids)) { break; } if (!$file->is_directory()) { $key = array_search($file->get_id(), $pids); if ($key !== false) { $file->delete(); unset($pids[$key]); } } } } } return true; }
/** * */ public function get_df() { return \mod_dataform_dataform::instance($this->dataid); }
/** * Throws an exception if the capability is not granted in applicable rules. * * @param string $capability Capability name. * @param array $params * @return void */ public static function require_capability($capability, array $params) { $dataformid = $params['dataformid']; $accessman = \mod_dataform_access_manager::instance($dataformid); $accesstype = get_called_class(); $rulesapplied = false; if ($rules = $accesstype::get_rules($accessman, $params)) { foreach ($rules as $rule) { if ($rule->is_enabled() and $rule->is_applicable($params)) { $rulesapplied = true; $rule->require_capability($capability, $params); } } } if (!$rulesapplied) { $dataformcontext = \mod_dataform_dataform::instance($dataformid)->context; require_capability($capability, $dataformcontext); } }
/** * */ public function set_quick_filter($view) { $df = mod_dataform_dataform::instance($this->_dataformid); $dfid = $df->id; $viewid = $view->id; $filterid = self::USER_FILTER_QUICK; $instance = get_user_preferences("dataform-filter-{$dfid}-{$viewid}-{$filterid}", null); $filteroptions = (object) self::get_filter_options_from_url(); // Neither saved filter nor new options. if (empty($instance) and empty($filteroptions)) { return null; } // Saved filter but no new options. if ($instance and empty($filteroptions)) { $instance = unserialize(base64_decode($instance)); return $instance; } // Saved filter and new options. if ($instance and $filteroptions) { $instance = (object) unserialize(base64_decode($instance)); // Add options to the existing filter and save. foreach ($filteroptions as $option => $val) { // Skip id. if ($option == 'id') { continue; } // Reset search if needed. if ($option == 'searchreset' and empty($filteroptions->search)) { $instance->search = ''; continue; } // Merge filters. if ($option == 'filters') { if (empty($instance->filters)) { $instance->filters = $val; } else { $instance->filters = array_merge($instance->filters, $val); } continue; } $instance->{$option} = $val; } $instance->dataid = $dfid; set_user_preference("dataform-filter-{$dfid}-{$viewid}-{$filterid}", base64_encode(serialize($instance))); return $instance; } // New options only. $filteroptions->id = $filterid; $filteroptions->dataid = $dfid; set_user_preference("dataform-filter-{$dfid}-{$viewid}-{$filterid}", base64_encode(serialize($filteroptions))); return $filteroptions; }
/** * Generates a dataform grade item. * @param array|stdClass $record * @param array $options * @return \grade_item generated object */ public function create_grade_item($record, array $options = null) { $record = (object) (array) $record; $dataformid = $record->dataid; $df = \mod_dataform_dataform::instance($dataformid); $grademan = $df->grade_manager; $itemnumber = $record->itemnumber; $itemname = $record->name; $grade = $record->grade; $gradecalc = !empty($record->gradecalc) ? $record->gradecalc : null; $gradeguide = !empty($record->gradeguide) ? $record->gradeguide : null; $gradedata = (object) array('grade' => $grade); $gradeparams = $grademan->get_grade_item_params_from_data($gradedata); $details = array(); $details['itemnumber'] = (int) $itemnumber; $details['itemname'] = $itemname; $details['gradecalc'] = $gradecalc; $details['gradeguide'] = $gradeguide; $details = array_merge($details, $gradeparams); $grademan->update_grade_item($itemnumber, $details); // Update instance settings (e.g. grade calc). $grademan->adjust_dataform_settings($itemnumber, $details); //$grademan->grade_items = null; //return $grademan->grade_items[$itemnumber]; }
/** * */ public function get_sql() { $this->init_filter_sql(); // Get all fields (see CONTRIB-5225). $df = \mod_dataform_dataform::instance($this->dataid); $fields = $df->field_manager->get_fields(array('forceget' => true)); // Get content fields. $fieldkeys = $this->contentfields ? array_fill_keys($this->contentfields, null) : null; $contentfields = $fieldkeys ? array_intersect_key($fields, $fieldkeys) : array(); // SEARCH sql. $searchsql = $this->get_search_sql($fields); list($searchtables, $searchwhere, $searchparams) = $searchsql; // SORT sql. $sortsql = $this->get_sort_sql($fields); list($sorttables, $sortwhere, $sortorder, $sortparams) = $sortsql; // CONTENT sql ($dataformcontent is an array of fieldid whose content needs to be fetched). $contentsql = $this->get_content_sql($contentfields); list($contentwhat, $contenttables, $contentwhere, $contentparams, $dataformcontent) = $contentsql; // JOIN sql (does't use params). list($joinwhat, $jointables, ) = $this->get_join_sql($fields); return array($searchtables, $searchwhere, $searchparams, $sorttables, $sortwhere, $sortorder, $sortparams, $contentwhat, $contenttables, $contentwhere, $contentparams, $dataformcontent, $joinwhat, $jointables); }
/** * Sets the js template of the specified dataform to the text passed as PyStringNode. * * @Given /^dataform "(?P<dataform_id_string>(?:[^"]|\\")*)" has the following js:$/ * @param string $dataformid * @param PyStringNode $content */ public function dataform_has_the_following_js($dataformid, PyStringNode $content) { $rec = new stdClass(); $rec->js = (string) $content; $df = mod_dataform_dataform::instance($dataformid); $df->update($rec); }
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * This page receives entry author asign me requests. * * @package dataformfield_entryauthor * @copyright 2014 Itamar Tzadok * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once '../../../../config.php'; $d = required_param('d', PARAM_INT); $viewid = required_param('vid', PARAM_INT); $entryid = required_param('eid', PARAM_INT); $action = required_param('action', PARAM_ALPHA); $ret = required_param('ret', PARAM_RAW); // Instantiate the Dataform. $df = mod_dataform_dataform::instance($d); require_login($df->course->id, false, $df->cm); // Sesskey. require_sesskey(); $PAGE->set_context($df->context); $PAGE->set_url('/mod/dataform/field/entryauthor/assign.php'); $fieldid = dataformfield_entryauthor_entryauthor::INTERNALID; $field = $df->field_manager->get_field_by_id($fieldid); // Assign. if ($action == 'assign') { $field->assign_user($USER->id, $entryid, $viewid); } // Unassign. if ($action == 'unassign') { $field->assign_user(0, $entryid, $viewid); }
/** * */ public function render_pagingbar($view) { $df = mod_dataform_dataform::instance($view->dataid); $output = $df->get_renderer(); $filter = $view->get_filter(); $baseurl = $view->get_baseurl(); // Typical groupby, one group per page case. show paging bar as per number of groups. if ($filter->pagenum) { $pagingbar = new paging_bar($filter->pagenum, $filter->page, 1, $baseurl . '&', 'page', '', true); return $output->render($pagingbar); } // Standard paging bar case. if ($filter->perpage) { $entryman = $view->entry_manager; $filteredcount = $entryman->entries ? $entryman->get_count(mod_dataform_entry_manager::COUNT_FILTERED) : 0; $displayedcount = $entryman->entries ? $entryman->get_count(mod_dataform_entry_manager::COUNT_DISPLAYED) : 0; // Adjust filter page if needed. // This may be needed if redirecting from entry form to paged view. if ($filter->eids and !$filter->page) { if ($entryid = is_array($filter->eids) ? reset($filter->eids) : $filter->eids and $entryid > 0) { $filter->page = $entryman->get_entry_position($entryid, $filter); } } if ($filteredcount and $displayedcount and $filteredcount != $displayedcount) { $url = new moodle_url($baseurl, array('filter' => $filter->id)); $pagingbar = new paging_bar($filteredcount, $filter->page, $filter->perpage, $url . '&', 'page', '', true); return $output->render($pagingbar); } } return null; }
require_once '../../../config.php'; $urlparams = new stdClass(); $urlparams->d = optional_param('d', 0, PARAM_INT); $urlparams->id = optional_param('id', 0, PARAM_INT); // Filters list actions. $urlparams->default = optional_param('default', 0, PARAM_INT); // id of filter to default $urlparams->showhide = optional_param('showhide', 0, PARAM_SEQUENCE); // filter ids (comma delimited) to hide/show $urlparams->delete = optional_param('delete', 0, PARAM_SEQUENCE); // filter ids (comma delim) to delete $urlparams->duplicate = optional_param('duplicate', 0, PARAM_SEQUENCE); // filter ids (comma delim) to duplicate $urlparams->confirmed = optional_param('confirmed', 0, PARAM_INT); // Set a dataform object. $df = mod_dataform_dataform::instance($urlparams->d, $urlparams->id); $df->require_manage_permission('filters'); $df->set_page('filter/index', array('urlparams' => $urlparams)); // Activate navigation node. navigation_node::override_active_url(new moodle_url('/mod/dataform/filter/index.php', array('id' => $df->cm->id))); $fm = mod_dataform_filter_manager::instance($df->id); // DATA PROCESSING. if ($urlparams->duplicate and confirm_sesskey()) { // Duplicate any requested filters. $fm->process_filters('duplicate', $urlparams->duplicate, $urlparams->confirmed); } else { if ($urlparams->delete and confirm_sesskey()) { // Delete any requested filters. $fm->process_filters('delete', $urlparams->delete, $urlparams->confirmed); } else { if ($urlparams->showhide and confirm_sesskey()) {
/** * Adding/updating entry record. * Assumes that permissions check has already been done. * * @param stdClass entry to add/update * @param stdClass data to update entry with * @param bool whether to update entry time * @return int|bool entry id if success, false otherwise */ protected function update_entry(&$entry, $data = null, $updatetime = true) { global $CFG, $DB, $USER; $df = mod_dataform_dataform::instance($this->dataformid); if ($data) { foreach ($data as $key => $value) { if ($key == 'name') { $entry->userid = $value; } else { $entry->{$key} = $value; } } // Don't update time later if set from data. if (isset($data['timemodified'])) { $updatetime = false; } // TODO Entry group sanity checks on $data['groupid']. } // Update existing entry (only authenticated users). if ($entry->id > 0) { if ($updatetime) { $entry->timemodified = time(); } if ($DB->update_record('dataform_entries', $entry)) { return $entry->id; } else { return false; } } // Add new entry (authenticated or anonymous (if enabled)) // Identify non-logged-in users (in anonymous entries) as guests. $currentuserid = empty($USER->id) ? $CFG->siteguest : $USER->id; $entryuserid = !empty($entry->userid) ? $entry->userid : $currentuserid; $entry->dataid = $df->id; $entry->userid = $df->grouped ? 0 : $entryuserid; $entry->groupid = !isset($entry->groupid) ? $df->currentgroup : $entry->groupid; $entry->timecreated = !isset($entry->timecreated) ? time() : $entry->timecreated; $entry->timemodified = !isset($entry->timemodified) ? time() : $entry->timemodified; $entry->state = !empty($entry->state) ? $entry->state : 0; $entry->id = $DB->insert_record('dataform_entries', $entry); return $entry->id; }
/** * {@link mod_dataform\interfaces\rss::get_rss_header_title()} */ public function get_rss_header_title() { $df = mod_dataform_dataform::instance($this->dataid); if ($this->param4) { $title = format_string($this->param4, true, array('context' => $df->context)); } else { $course = $df->course; $coursecontext = context_course::instance($course->id); $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext)); $title = $courseshortname . ': ' . format_string($df->name, true, array('context' => $df->context)); } return $title; }
function xmldb_dataform_upgrade_2014111000($dbman, $oldversion) { global $CFG, $DB; $newversion = 2014111000; if ($oldversion < $newversion) { // Replace field template pattern from [[fieldname@]] to [[T@fieldname]]. if ($dataforms = $DB->get_records('dataform')) { foreach (array_keys($dataforms) as $dataformid) { $sqlparams = array('dataid' => $dataformid); // Get field names of the dataform fields. if (!($fieldnames = $DB->get_records_menu('dataform_fields', $sqlparams, '', 'id,name'))) { continue; } // Must have views to continue. if (!$DB->record_exists('dataform_views', $sqlparams)) { continue; } $df = mod_dataform_dataform::instance($dataformid); $replacements = array(); foreach ($fieldnames as $fieldname) { $replacements["[[{$fieldname}@]]"] = "[[T@{$fieldname}]]"; } $df->view_manager->replace_patterns_in_views(array_keys($replacements), $replacements); } } // Enable existing field plugins. $type = 'dataformfield'; $enabled = array_keys(core_component::get_plugin_list($type)); set_config("enabled_{$type}", implode(',', $enabled), 'mod_dataform'); // Enable existing view plugins. $type = 'dataformview'; $enabled = array_keys(core_component::get_plugin_list($type)); set_config("enabled_{$type}", implode(',', $enabled), 'mod_dataform'); // Add defaultcontentmode column to dataform_fields. $table = new xmldb_table('dataform_fields'); $field = new xmldb_field('defaultcontentmode', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'label'); if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Add defaultcontent column to dataform_fields. $table = new xmldb_table('dataform_fields'); $field = new xmldb_field('defaultcontent', XMLDB_TYPE_TEXT, null, null, null, null, null, 'defaultcontentmode'); if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Dataform savepoint reached. upgrade_mod_savepoint(true, $newversion, 'dataform'); } return true; }