protected function process_customlabel($data) { global $DB; $data = (object) $data; $oldid = $data->id; $data->course = $this->get_courseid(); $data->timemodified = $this->apply_date_offset($data->timemodified); // check the label subclass and fallback if not available here // disabled classes are still restored $classes = customlabel_get_classes(null, false, true); if (!array_key_exists($data->labelclass, $classes)) { if (!empty($data->fallbacktype)) { $data->labelclass = $data->fallbacktype; $data->fallbacktype = ''; } else { $data->labelclass = 'text'; $data->fallbacktype = ''; } } // insert the data record $newitemid = $DB->insert_record('customlabel', $data); $this->apply_activity_instance($newitemid); }
protected function process_customlabel($data) { global $DB; static $classes = null; static $systemcontext = null; // always restore at system level context. Everything should pass. if (is_null($systemcontext)) { $systemcontext = context_system::instance(); } if (is_null($classes)) { $classes = customlabel_get_classes($systemcontext, false, 'names'); } $data = (object) $data; $oldid = $data->id; $data->course = $this->get_courseid(); $data->timemodified = $this->apply_date_offset($data->timemodified); // check the label subclass and fallback if not available here // disabled classes are still restored if (!in_array($data->labelclass, $classes)) { if (!empty($data->fallbacktype)) { $data->labelclass = $data->fallbacktype; $data->fallbacktype = ''; } else { $data->labelclass = 'text'; $data->fallbacktype = ''; } } $data->intro = ''; $data->introformat = 0; // insert the data record $newitemid = $DB->insert_record('customlabel', $data); // postupdate name $this->__postupdate($data, 'name', $oldid, $newitemid); $this->__postupdate($data, 'title', $oldid, $newitemid); $this->apply_activity_instance($newitemid); }
public function definition() { global $COURSE, $DB, $CFG, $SESSION; $context = context_course::instance($COURSE->id); // Get classes for custom label. $labelclasses = customlabel_get_classes($context); $qoptions = array(); foreach ($labelclasses as $labelclass) { $qoptions[$labelclass->family][$labelclass->id] = $labelclass->name; } asort($qoptions); $mform = $this->_form; $mform->disable_form_change_checker(); $tomodel = ''; $customlabel = new StdClass(); if ($tomodel = optional_param('type', '', PARAM_TEXT)) { $customlabel->labelclass = $tomodel; } elseif ($tomodel = @$SESSION->customlabel->update_type_change) { $customlabel->labelclass = $tomodel; unset($SESSION->customlabel); } $customlabel->processedcontent = ''; if (!$tomodel) { if ($this->current->instance) { // Are we updating an existing instance ? $customlabel = $DB->get_record('customlabel', array('id' => $this->current->id)); } else { $customlabel->title = ''; $customlabel->labelclass = 'text'; } } $customclass = customlabel_load_class($customlabel); $mform->addElement('hidden', 'intro', ''); $mform->setType('intro', PARAM_TEXT); $mform->addElement('hidden', 'introformat', 0); $mform->setType('introformat', PARAM_INT); $section = optional_param('section', 0, PARAM_INT); $returntomod = optional_param('return', 0, PARAM_BOOL); if (has_capability('mod/customlabel:fullaccess', $context) || $customclass->fullaccess) { $onchangeadvicestr = str_replace("'", "\\'", get_string('changetypeadvice', 'customlabel')); // $mform->addElement('select', 'labelclass', get_string('labelclass', 'customlabel'), $qoptions, array('onchange' => "type_change_submit(this,'$onchangeadvicestr')", 'id' => 'menulabelclass')); $labelid = 0 + @$this->current->update; $typeselect =& $mform->addElement('select', 'labelclass', get_string('labelclass', 'customlabel'), array(), array('onchange' => "type_change_submit('{$onchangeadvicestr}', '{$COURSE->id}', '{$section}', '{$returntomod}', '" . sesskey() . "', '" . $labelid . "')", 'id' => 'menulabelclass')); foreach ($qoptions as $family => $options) { if (!empty($family)) { if (!preg_match('/^\\[.*\\]$/', $family)) { $strkey = 'family' . $family; $familyname = get_string($strkey, 'customlabel'); $typeselect->addOption('--- ' . $familyname . ' ---', '', array('disabled' => 'disabled')); } else { // Possible unclassified element. Should we notice that ? } } foreach ($options as $opt => $optlabel) { $typeselect->addOption($optlabel, $opt); } } $mform->setType('labelclass', PARAM_TEXT); $mform->setDefault('labelclass', 'text'); } else { $mform->addElement('static', 'labelclassname', get_string('labelclass', 'customlabel')); $mform->addElement('hidden', 'labelclass'); $mform->setType('labelclass', PARAM_TEXT); } $mform->addElement('text', 'title', get_string('title', 'customlabel')); $customlabel_next_id = $DB->get_field('customlabel', 'MAX(id)', array()) + 1; $mform->setDefault('title', $customlabel->labelclass . '_' . $customlabel_next_id); $mform->setType('title', PARAM_TEXT); if ($customlabel->labelclass == 'text') { $editoroptions = self::editor_options(); $editoroptions['context'] = $this->context; $mform->addElement('editor', 'textcontent_editor', get_string('content', 'customlabel'), null, $editoroptions); } else { if (!$customclass) { print_error("Custom label class lacks of definition"); } foreach ($customclass->fields as $field) { // No capable users cannot edit lock fields. if (!has_capability('mod/customlabel:fullaccess', $context) && !empty($field->admin)) { continue; } $fieldname = str_replace('[]', '', $field->name); // Must take care it is a multiple field. $fieldlabel = get_string($field->name, 'customlabeltype_' . $customclass->type); if ($field->type == 'choiceyesno') { $mform->addElement('selectyesno', $field->name, $fieldlabel); $mform->setType($field->name, PARAM_BOOL); } elseif ($field->type == 'textfield') { $attrs = array('size' => @$field->size, 'maxlength' => @$field->maxlength); $mform->addElement('text', $field->name, $fieldlabel, $attrs); $mform->setType($field->name, PARAM_CLEANHTML); } elseif ($field->type == 'editor' || $field->type == 'textarea') { $editoroptions = self::editor_options(); $editoroptions['context'] = $this->context; $mform->addElement('editor', $field->name . '_editor', $fieldlabel, null, $editoroptions); } elseif (preg_match("/list\$/", $field->type)) { if (empty($field->straightoptions)) { $options = $customclass->get_options($fieldname); } else { $options = array_combine($field->options, $field->options); } $select =& $mform->addElement('select', $field->name, $fieldlabel, $options); if (!empty($field->multiple)) { $select->setMultiple(true); } $mform->setType($field->name, PARAM_TEXT); } elseif (preg_match("/datasource\$/", $field->type)) { // Very similar to lists, except options come from an external datasource $options = $customclass->get_datasource_options($field); $script = ''; if (!empty($field->constraintson)) { $script = " applyconstraints('{$CFG->wwwroot}', '{$customclass->type}', this, '{$field->constraintson}'); "; } $attrs = array('onchange' => $script); $select =& $mform->addElement('select', $field->name, $fieldlabel, $options, $attrs); if (!empty($field->multiple)) { $select->setMultiple(true); } $mform->setType($field->name, PARAM_TEXT); } elseif ($field->type == 'filepicker') { $group = array(); $types = !empty($field->acceptedtypes) ? $field->acceptedtypes : '*'; $group[] = $mform->createElement('filepicker', $field->name, '', array('courseid' => $COURSE->id, 'accepted_types' => $types)); $group[] = $mform->createElement('checkbox', 'clear' . $field->name, '', get_string('cleararea', 'customlabel')); $mform->addGroup($group, $field->name . 'group', $fieldlabel, '', array(''), false); } else { echo "Unknown or unsupported type : {$field->type}"; } if (isset($field->mandatory)) { $mform->addRule($field->name, null, 'required', null, 'client'); } if (!empty($field->help)) { $mform->addHelpButton($field->name, $field->help, 'customlabeltype_' . $customlabel->labelclass); } $mform->setDefault($fieldname, @$field->default); } } //------------------------------------------------------------------------------- $this->standard_coursemodule_elements(); //------------------------------------------------------------------------------- // buttons $this->add_action_buttons(); }
/** * Use by customlabel updater or by manual mass regeneration tools. * This may be needed if an administrator changes the elements templates * and need to recalculate all the course elements to use this change. * This function operzates in one single course * @param int $course the course id where to operate * @param mixed $labelclasses 'all' or an array of class names to operate in the course. */ function customlabel_course_regenerate(&$course, $labelclasses = '') { global $DB; if ($labelclasses == 'all') { // Take them all. $labelclasses = customlabel_get_classes(context_system::instance(), false, 'names'); } foreach ($labelclasses as $labelclassname) { mtrace(" processing class '{$labelclassname}'"); $customlabels = $DB->get_records_select('customlabel', " course = ? AND labelclass = ? ", array($course->id, $labelclassname)); if ($customlabels) { foreach ($customlabels as $customlabel) { customlabel_regenerate($customlabel, $labelclassname, $course); } } } }
<?php include_once $CFG->dirroot . '/mod/customlabel/locallib.php'; $systemcontext = context_system::instance(); $classes = customlabel_get_classes(null, false); $roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL); $rolemenu = array(); foreach ($roles as $rid => $role) { $rolemenu[$rid] = $role->localname; } $settings->add(new admin_setting_heading('regeneration', get_string('regeneration', 'customlabel'), "<a href=\"{$CFG->wwwroot}/mod/customlabel/admin_updateall.php\">" . get_string('regenerate', 'customlabel') . "</a>")); $settings->add(new admin_setting_heading('classification', get_string('classification', 'customlabel'), "<a href=\"{$CFG->wwwroot}/mod/customlabel/adminmetadata.php\">" . get_string('classification', 'customlabel') . "</a>")); $settings->add(new admin_setting_heading('roleaccesstoelements', get_string('roleaccesstoelements', 'customlabel'), '')); foreach ($classes as $class) { $parmname = "customlabel_{$class->id}_enabled"; $description = get_string('enabletype', 'customlabel') . ' ' . get_string('typename', 'customlabeltype_' . $class->id); $settings->add(new admin_setting_configcheckbox("list_{$parmname}", get_string('typename', 'customlabeltype_' . $class->id), get_string('configtypename', 'customlabeltype_' . $class->id), 1)); $description = get_string('hiddenrolesfor', 'customlabel') . ' ' . get_string('typename', 'customlabeltype_' . $class->id); $parmname = "customlabel_{$class->id}_hiddenfor"; $selection = explode(',', @$CFG->{$parmname}); $settings->add(new admin_setting_configmultiselect("{$parmname}", "customlabel_{$class->id}_hiddenfor", $description, $selection, $rolemenu)); }
// admin_externalpage_print_header(); $systemcontext = context_system::instance(); require_login(); require_capability('moodle/site:config', $systemcontext); $url = $CFG->wwwroot . '/mod/customlabel/admin_updateall.php'; $PAGE->set_context($systemcontext); $PAGE->set_url($url); $PAGE->set_title(get_string('updatelabels', 'customlabel', get_string('pluginname', 'customlabel'))); $PAGE->set_heading(get_string('updatelabels', 'customlabel', get_string('pluginname', 'customlabel'))); /* SCANMSG: may be additional work required for $navigation variable */ $PAGE->set_focuscontrol(''); $PAGE->set_cacheable(true); /// get courses $allcourses = $DB->get_records_menu('course', null, 'shortname', 'id,shortname'); /// get types $alltypes = customlabel_get_classes(); if ($alltypes) { foreach ($alltypes as $atype) { $types[$atype->id] = $atype->name; } } else { $types = array(); } /// if data submitted, proceed $form = new customlabel_updateall_form($allcourses, $types); if ($form->is_cancelled()) { redirect($CFG->wwwroot . '/admin/settings.php?section=modsettingcustomlabel'); } echo $OUTPUT->header(); if ($data = $form->get_data()) { echo $OUTPUT->container_start('emptyleftspace');
require_once $CFG->dirroot . '/mod/customlabel/admin_updateall_form.php'; $systemcontext = context_system::instance(); require_login(); require_capability('moodle/site:config', $systemcontext); $url = $CFG->wwwroot . '/mod/customlabel/admin_updateall.php'; $PAGE->set_context($systemcontext); $PAGE->set_url($url); $PAGE->set_title(get_string('updatelabels', 'customlabel', get_string('pluginname', 'customlabel'))); $PAGE->set_heading(get_string('updatelabels', 'customlabel', get_string('pluginname', 'customlabel'))); /* SCANMSG: may be additional work required for $navigation variable */ $PAGE->set_focuscontrol(''); $PAGE->set_cacheable(true); // Get courses. $allcourses = $DB->get_records_menu('course', null, 'shortname', 'id,shortname'); // Get types. $alltypes = customlabel_get_classes($systemcontext, false); if ($alltypes) { foreach ($alltypes as $atype) { $types[$atype->id] = $atype->name; } } else { $types = array(); } // If data submitted, proceed. $form = new customlabel_updateall_form($url, array('courses' => $allcourses, 'types' => $types)); if ($form->is_cancelled()) { redirect($CFG->wwwroot . '/admin/settings.php?section=modsettingcustomlabel'); } echo $OUTPUT->header(); if ($data = $form->get_data()) { echo $OUTPUT->container_start('emptyleftspace');