/** * 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); } }
$urlparams->cancel = optional_param('cancel', 0, PARAM_BOOL); // Enable context (show block). $urlparams->enable = optional_param('enable', 0, PARAM_INT); // Disable context (hide block). $urlparams->disable = optional_param('disable', 0, PARAM_INT); // Delete context (delete block). $urlparams->delete = optional_param('delete', 0, PARAM_INT); $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('access'); $df->set_page('access/index', array('urlparams' => $urlparams)); $PAGE->set_context($df->context); // Activate navigation node. navigation_node::override_active_url(new moodle_url('/mod/dataform/access/index.php', array('id' => $df->cm->id))); $aman = mod_dataform_access_manager::instance($df->id); // DATA PROCESSING // Enable. if ($urlparams->enable and confirm_sesskey()) { $aman->set_rule_visibility($urlparams->enable, 1); } // Disable. if ($urlparams->disable and confirm_sesskey()) { $aman->set_rule_visibility($urlparams->disable, 0); } // Delete. if ($urlparams->delete and confirm_sesskey()) { $aman->delete_rule($urlparams->delete); } $output = $df->get_renderer(); echo $output->header(array('tab' => 'access', 'heading' => $df->name, 'urlparams' => $urlparams));
/** * @return null|array */ public static function get_rules(\mod_dataform_access_manager $man, array $params) { return array_merge($man->get_type_rules('view'), $man->get_type_rules('entry')); }
/** * Add/update entries * * @param array Array of entries to process * @param stdClass Entries data * @return array Array of processed entries */ protected function process_update_entries($entries, $data) { if (empty($entries) or is_null($data)) { return array(); } $df = mod_dataform_dataform::instance($this->dataformid); $accessman = mod_dataform_access_manager::instance($this->dataformid); // Check permissions. foreach ($entries as $entryid => $entry) { $entrydo = $entryid < 0 ? 'mod_dataform\\access\\entry_add' : 'mod_dataform\\access\\entry_update'; $accessparams = array('dataformid' => $this->dataformid, 'viewid' => $this->viewid); if (!$entrydo::validate($accessparams + array('entry' => $entry))) { unset($entries[$entryid]); } } // In case none remain for processing. if (empty($entries)) { return array(); } $processed = array(); // First parse the data to collate content in an array for each recognized field. $contents = array_fill_keys(array_keys($entries), array('info' => array(), 'fields' => array())); $calculations = array(); $fields = $this->field_manager->get_fields(); $savetype = ''; // Iterate the data and extract entry and fields content. foreach ($data as $name => $value) { // Which submit type. if (strpos($name, 'submitbutton') === 0) { list(, $savetype, ) = explode('_', $name); continue; } if (strpos($name, 'entry_') === 0) { // Entry info // Assuming only entry info names start with entry_. list(, $entryid, $var) = explode('_', $name); $contents[$entryid]['info'][$var] = $value; } else { if (strpos($name, 'field_') === 0) { // Assuming only field names contain field_. list(, $fieldid, $entryid) = explode('_', $name); if (!empty($fields[$fieldid])) { $field = $fields[$fieldid]; } else { if ($field = $this->field_manager->get_field_by_id($fieldid)) { $fields[$fieldid] = $field; } else { continue; } } // Entry content. if (!array_key_exists($fieldid, $contents[$entryid]['fields'])) { $contents[$entryid]['fields'][$fieldid] = $field->get_content_from_data($entryid, $data); } } } } // Now update entry and contents. $savenew = strpos($savetype, 'savenew') === 0; foreach ($entries as $eid => $entry) { if ($savenew) { $entry->id = 0; unset($entry->timecreated); unset($entry->timemodified); } if ($entry->id = $this->update_entry($entry, $contents[$eid]['info'])) { // $eid should be different from $entryid only in new entries. foreach ($contents[$eid]['fields'] as $fieldid => $content) { $fields[$fieldid]->update_content($entry, $content, $savenew); } // Trigger the entry event. if ($eid != $entry->id) { $entryevent = '\\mod_dataform\\event\\entry_created'; } else { $entryevent = '\\mod_dataform\\event\\entry_updated'; } $eventparams = array('objectid' => $entry->id, 'context' => $df->context, 'relateduserid' => $entry->userid, 'other' => array('dataid' => $this->dataformid, 'viewid' => $this->viewid, 'entryid' => $entry->id)); $event = $entryevent::create($eventparams); $event->add_record_snapshot('dataform_entries', $entry); $event->trigger(); // Update calculated grades if applicable. $df->grade_manager->update_calculated_grades($entry); $processed[$entry->id] = $entry; } } return $processed; }
/** * @return null|array */ public static function get_rules(\mod_dataform_access_manager $man, array $params) { return $man->get_type_rules('view'); }
/** * Returns html for admin style list of fields. * * @param string $extorint Subset type external|internal * @param string $heading Title of list * @param array $fields List of available fields * @return string HTML fragment of html_table */ public function fields_admin_list($extorint, $heading, $fields) { if (!$this->_dataformid) { return null; } $df = mod_dataform_dataform::instance($this->_dataformid); // External or internal. $external = $extorint == 'external'; $editbaseurl = new moodle_url('/mod/dataform/field/edit.php', array('d' => $this->_dataformid)); $actionbaseurl = new moodle_url('/mod/dataform/field/index.php', array('d' => $this->_dataformid)); $sessparam = array('sesskey' => sesskey()); $strname = get_string('name'); $strdescription = get_string('description'); $strtype = get_string('type', 'dataform'); $stredit = get_string('edit'); $strduplicate = get_string('duplicate'); $strdelete = get_string('delete'); $strhide = get_string('hide'); $strshow = get_string('show'); $strlock = get_string('lock', 'dataform'); $strunlock = get_string('unlock', 'dataform'); $strpermissions = get_string('permissions', 'role'); $strnotifications = get_string('notifications'); // Icons. $editicon = $this->output->pix_icon('t/edit', $stredit); $duplicateicon = $this->output->pix_icon('t/copy', $strduplicate); $deleteicon = $this->output->pix_icon('t/delete', $strdelete); $notificationicon = $this->output->pix_icon('notification', $strnotifications, 'dataform'); $nonotificationicon = $this->output->pix_icon('nonotification', $strnotifications, 'dataform'); $accessicon = $this->output->pix_icon('access', $strpermissions, 'dataform'); $noaccessicon = $this->output->pix_icon('noaccess', $strpermissions, 'dataform'); // The default value of the type attr of a button is submit, so set it to button so that // it doesn't submit the form. $selectallnone = html_writer::checkbox('fieldselectallnone', null, false, null, array('id' => 'id_fieldselectallnone')); $this->page->requires->js_init_call('M.mod_dataform.util.init_select_allnone', array('field')); $multiactionurl = new moodle_url($actionbaseurl, $sessparam); // Bulk delete. $icon = new pix_icon('t/delete', get_string('multidelete', 'dataform')); $multidelete = $this->output->action_icon($actionbaseurl, $icon, null, array('id' => 'id_field_bulkaction_delete')); $this->page->requires->js_init_call('M.mod_dataform.util.init_bulk_action', array('field', 'delete', $multiactionurl->out(false))); // Bulk duplicate. $icon = new pix_icon('t/copy', get_string('multiduplicate', 'dataform')); $multiduplicate = $this->output->action_icon($actionbaseurl, $icon, null, array('id' => 'id_field_bulkaction_duplicate')); $this->page->requires->js_init_call('M.mod_dataform.util.init_bulk_action', array('field', 'duplicate', $multiactionurl->out(false))); // Table headers. $headers = array('name' => array($strname, 'left', false), 'type' => array($strtype, 'left', false), 'description' => array($strdescription, 'left', false), 'visible' => array(get_string('visible'), 'center', false), 'editable' => array(get_string('fieldeditable', 'dataform'), 'center', false), 'info' => array(null, 'left', false), 'actions' => array("{$multiduplicate} {$multidelete} {$selectallnone}", 'right', false)); if (!$external) { unset($headers['actions']); unset($headers['visible']); unset($headers['editable']); } // Access manager. $aman = mod_dataform_access_manager::instance($this->_dataformid); // Notification manager. $nman = mod_dataform_notification_manager::instance($this->_dataformid); if (!$fields) { $fields = array(); } $rows = array(); foreach ($fields as $fieldid => $field) { if (!$field) { continue; } // Name. if ($field instanceof \mod_dataform\pluginbase\dataformfield_internal) { $fieldname = $field->name; } else { $fieldname = html_writer::link(new moodle_url($editbaseurl, $sessparam + array('fid' => $fieldid)), $field->name); } // Type. $fieldtype = $field->image . ' ' . $field->typename; // Description. $fielddescription = shorten_text($field->description, 30); // Visible. if ($visible = $field->visible) { $visibleicon = $this->output->pix_icon('t/hide', $strhide); $visibleicon = $visible == 1 ? "({$visibleicon})" : $visibleicon; } else { $visibleicon = $this->output->pix_icon('t/show', $strshow); } $fieldvisible = html_writer::link(new moodle_url($actionbaseurl, $sessparam + array('visible' => $fieldid)), $visibleicon); // Editable. if ($editable = $field->editable) { $editableicon = $this->output->pix_icon('t/lock', $strlock); } else { $editableicon = $this->output->pix_icon('t/unlock', $strunlock); } $fieldeditable = html_writer::link(new moodle_url($actionbaseurl, $sessparam + array('editable' => $fieldid)), $editableicon); // INFO // Access rules. if ($rulenames = $aman->get_field_rules($field->name)) { $fieldaccess = $accessicon; // Html_writer::alist($rulenames);. } else { $fieldaccess = $noaccessicon; } // Notification rules. if ($rulenames = $nman->get_field_rules($field->name)) { $fieldnotifications = $notificationicon; // Html_writer::alist($rulenames);. } else { $fieldnotifications = $nonotificationicon; } $fieldinfo = implode(' ', array($fieldaccess, $fieldnotifications)); // ACTIONS. if ($field instanceof \mod_dataform\pluginbase\dataformfield_internal) { $fieldactions = null; } else { $url = new moodle_url($editbaseurl, $sessparam + array('fid' => $fieldid)); $linkparams = array('id' => "id_editfield{$fieldid}", 'title' => "{$stredit} {$field->name}"); $fieldedit = html_writer::link($url, $editicon, $linkparams); $url = new moodle_url($actionbaseurl, $sessparam + array('duplicate' => $fieldid)); $linkparams = array('id' => "id_duplicatefield{$fieldid}", 'title' => "{$strduplicate} {$field->name}"); $fieldduplicate = html_writer::link($url, $duplicateicon, $linkparams); $url = new moodle_url($actionbaseurl, $sessparam + array('delete' => $fieldid)); $linkparams = array('id' => "id_deletefield{$fieldid}", 'title' => "{$strdelete} {$field->name}"); $fielddelete = html_writer::link($url, $deleteicon, $linkparams); $fieldselector = html_writer::checkbox("fieldselector", $fieldid, false, null, array('class' => 'fieldselector')); $fieldactions = implode(' ', array($fieldedit, $fieldduplicate, $fielddelete, $fieldselector)); } $data = array(); foreach (array_keys($headers) as $key) { $data[] = ${"field{$key}"}; } $rows[] = $data; } // Generate the table. $table = new html_table(); foreach ($headers as $header) { list($table->head[], $table->align[], $table->wrap[]) = $header; } $table->data = $rows; $title = $heading ? html_writer::tag('h3', $heading) : null; return $title . html_writer::table($table); }
/** * Deletes all this instance's structure and user data and resets its settings to defaults. * * @return bool Always true */ protected function reset() { // Must have manage templates capability. require_capability('mod/dataform:managetemplates', $this->context); // Reset settings. $this->reset_settings(); // Delete all component items. mod_dataform_field_manager::instance($this->id)->delete_fields(); mod_dataform_view_manager::instance($this->id)->delete_views(); mod_dataform_filter_manager::instance($this->id)->delete_filters(); mod_dataform_filter_manager::instance($this->id)->delete_advanced_filters(); mod_dataform_access_manager::instance($this->id)->delete_rules(); mod_dataform_notification_manager::instance($this->id)->delete_rules(); // Reset user data. $this->reset_user_data(); // Delete remaining files (e.g. css, js). $fs = get_file_storage(); $fs->delete_area_files($this->context->id, 'mod_dataform'); // Clean up gradebook. mod_dataform_grade_manager::instance($this->id)->delete_grade_items(); // Refresh events. dataform_refresh_events($this->course->id); // Delete context content. $this->context->delete_content(); // Update instance store. if (\mod_dataform_instance_store::instance($this->id, 'dataform')) { \mod_dataform_instance_store::register($this->id, 'dataform', $this); } return true; }