public function rebuild_artefact_list() { db_begin(); // Remember what was in this block before saving, and always allow those artefacts to remain // in it, regardless of the user's current permissions. $old = get_records_assoc('view_artefact', 'block', $this->id, '', 'artefact, id'); delete_records('view_artefact', 'block', $this->id); safe_require('blocktype', blocktype_name_to_namespaced($this->get('blocktype'))); if (!($artefacts = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'get_artefacts', $this))) { db_commit(); return true; } foreach ($artefacts as $key => $id) { if (!$id || intval($id) == 0) { log_warn("get_artefacts returned an invalid artefact ID for block instance {$this->id} (" . $this->get('blocktype') . ")"); unset($artefacts[$key]); } } if (count($artefacts) == 0) { db_commit(); return true; } // Get list of allowed artefacts require_once 'view.php'; $searchdata = array('extraselect' => array(array('fieldname' => 'id', 'type' => 'int', 'values' => $artefacts)), 'userartefactsallowed' => true); list($allowed, $count) = View::get_artefactchooser_artefacts($searchdata, $this->get_view()->get('owner'), $this->get_view()->get('group'), $this->get_view()->get('institution'), true); $va = new StdClass(); $va->view = $this->get('view'); $va->block = $this->id; foreach ($artefacts as $id) { if (isset($allowed[$id]) || isset($old[$id])) { $va->artefact = $id; insert_record('view_artefact', $va); } } db_commit(); }
/** * Builds the configuration pieform for this blockinstance * * @return array Array with two keys: 'html' for raw html, 'javascript' for * javascript to run */ public function build_configure_form($new = false) { static $renderedform; if (!empty($renderedform)) { return $renderedform; } safe_require('blocktype', $this->get('blocktype')); $elements = call_static_method(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_form', $this, $this->get_view()->get('template')); $blocktypeclass = generate_class_name('blocktype', $this->get('blocktype')); if ($this->get('title') != call_static_method($blocktypeclass, 'get_title')) { // If the title for this block has been set to something other than // the block title, use it unconditionally $title = $this->get('title'); } else { if (method_exists($blocktypeclass, 'get_instance_title')) { // Block types can specify a default title for a block $title = call_static_method($blocktypeclass, 'get_instance_title', $this); } else { // A block that doesn't have a method for setting an instance // title, and hasn't had its title changed (e.g. a new textbox) $title = $this->get('title'); } } $elements = array_merge(array('title' => array('type' => 'text', 'title' => get_string('blocktitle', 'view'), 'description' => method_exists($blocktypeclass, 'get_instance_title') ? get_string('defaulttitledescription', 'blocktype.' . blocktype_name_to_namespaced($this->get('blocktype'))) : null, 'defaultvalue' => $title), 'blockconfig' => array('type' => 'hidden', 'value' => $this->get('id')), 'id' => array('type' => 'hidden', 'value' => $this->get('view')), 'change' => array('type' => 'hidden', 'value' => 1), 'new' => array('type' => 'hidden', 'value' => $new)), $elements); if ($new) { $cancel = get_string('remove'); $elements['removeoncancel'] = array('type' => 'hidden', 'value' => 1); $elements['sure'] = array('type' => 'hidden', 'value' => 1); } else { $cancel = get_string('cancel'); } // Add submit/cancel buttons $elements['action_configureblockinstance_id_' . $this->get('id')] = array('type' => 'submitcancel', 'value' => array(get_string('save'), $cancel), 'goto' => View::make_base_url()); $configdirs = array(get_config('libroot') . 'form/'); if ($this->get('artefactplugin')) { $configdirs[] = get_config('docroot') . 'artefact/' . $this->get('artefactplugin') . '/form/'; } $form = array('name' => 'instconf', 'renderer' => 'maharatable', 'validatecallback' => array(generate_class_name('blocktype', $this->get('blocktype')), 'instance_config_validate'), 'successcallback' => array($this, 'instance_config_store'), 'jsform' => true, 'jssuccesscallback' => 'blockConfigSuccess', 'jserrorcallback' => 'blockConfigError', 'elements' => $elements, 'viewgroup' => $this->get_view()->get('group'), 'group' => $this->get_view()->get('group'), 'viewinstitution' => $this->get_view()->get('institution'), 'institution' => $this->get_view()->get('institution'), 'configdirs' => $configdirs, 'plugintype' => 'blocktype', 'pluginname' => $this->get('blocktype')); if (param_variable('action_acsearch_id_' . $this->get('id'), false)) { $form['validate'] = false; } require_once 'pieforms/pieform.php'; $pieform = new Pieform($form); if ($pieform->is_submitted()) { global $SESSION; $SESSION->add_error_msg(get_string('errorprocessingform')); } $html = $pieform->build(); // We probably need a new version of $pieform->build() that separates out the js // Temporary evil hack: if (preg_match('/<script type="text\\/javascript">(new Pieform\\(.*\\);)<\\/script>/', $html, $matches)) { $js = "var pf_{$form['name']} = " . $matches[1] . "pf_{$form['name']}.init();"; } else { $js = ''; } // We need to load any javascript required for the pieform. We do this // by checking for an api function that has been added especially for // the purpose, but that is not part of Pieforms. Maybe one day later // it will be though // $js = ''; foreach ($elements as $key => $element) { $element['name'] = $key; $function = 'pieform_element_' . $element['type'] . '_views_js'; if (is_callable($function)) { $js .= call_user_func_array($function, array($pieform, $element)); } } $renderedform = array('html' => $html, 'javascript' => $js); return $renderedform; }