Пример #1
0
 function display_default()
 {
     global $CFG, $DB, $OUTPUT;
     $level = $this->required_param('level', PARAM_ACTION);
     if (!($ctxlvl = \local_eliscore\context\helper::get_level_from_name($level))) {
         print_error('invalid_context_level', 'local_elisprogram');
     }
     $tmppage = new moodle_url($this->url);
     $tabs = array();
     $contextlevels = \local_eliscore\context\helper::get_legacy_levels();
     foreach ($contextlevels as $contextlevel => $val) {
         $tmppage->param('level', $contextlevel);
         $tabs[] = new tabobject($contextlevel, $tmppage->out(), get_string($contextlevel, 'local_elisprogram'));
     }
     print_tabs(array($tabs), $level);
     $fields = field::get_for_context_level($ctxlvl);
     $fields = $fields ? $fields : array();
     $categories = field_category::get_for_context_level($ctxlvl);
     $categories = $categories ? $categories : array();
     // divide the fields into categories
     $category_names = array();
     $fieldsbycategory = array();
     foreach ($categories as $category) {
         $category_names[$category->id] = $category->name;
         $fieldsbycategory[$category->id] = array();
     }
     foreach ($fields as $field) {
         $fieldsbycategory[$field->categoryid][] = $field;
     }
     $deletetxt = get_string('delete');
     $edittxt = get_string('edit');
     $syncerr = false;
     if (empty($category_names)) {
         echo $OUTPUT->heading(get_string('field_no_categories_defined', 'local_elisprogram'));
     }
     foreach ($fieldsbycategory as $categoryid => $fields) {
         $categorypage = new moodle_url($this->url);
         $categorypage->params(array('action' => 'deletecategory', 'id' => $categoryid, 'level' => $level));
         $deletelink = $categorypage->out();
         $categorypage->param('action', 'editcategory');
         $editlink = $categorypage->out();
         if (isset($category_names[$categoryid])) {
             echo "<h2>{$category_names[$categoryid]} ";
             echo "<a href=\"{$editlink}\" class=\"elisicon elisicon-edit\" alt=\"{$edittxt}\" title=\"{$edittxt}\"></a>";
             echo "<a href=\"{$deletelink}\" class=\"elisicon elisicon-remove\" alt=\"{$deletetxt}\" title=\"{$deletetxt}\"></a>";
             echo "</h2>\n";
         }
         if (empty($fields)) {
             print_string('field_no_fields_defined', 'local_elisprogram');
         } else {
             if ($level == 'user') {
                 require_once elis::plugin_file('elisfields_moodleprofile', 'custom_fields.php');
                 $table = new customuserfieldtable($fields, array('name' => array('header' => get_string('name')), 'datatype' => array('header' => get_string('field_datatype', 'local_elisprogram')), 'syncwithmoodle' => array('header' => get_string('field_syncwithmoodle', 'local_elisprogram')), 'buttons' => array('header' => '')), $this->url);
             } else {
                 $table = new customfieldtable($fields, array('name' => array('header' => get_string('name')), 'datatype' => array('header' => 'Data type'), 'buttons' => array('header' => '')), $this->url);
             }
             echo $table->get_html();
             $syncerr = $syncerr || !empty($table->syncerr);
         }
     }
     if ($syncerr) {
         print_string('moodle_field_sync_warning', 'local_elisprogram');
     }
     // button for new category
     $options = array('s' => 'field', 'action' => 'editcategory', 'level' => $level);
     $button = new single_button(new moodle_url('index.php', $options), get_string('field_create_category', 'local_elisprogram'), 'get', array('disabled' => false, 'title' => get_string('field_create_category', 'local_elisprogram'), 'id' => ''));
     echo $OUTPUT->render($button);
     if (!empty($category_names)) {
         if ($level == 'user') {
             // create new field from Moodle field
             $sql = 'shortname NOT IN
                     (SELECT shortname FROM {' . field::TABLE . '} ef
                     INNER JOIN {' . field_contextlevel::TABLE . '} cl
                     ON ef.id = cl.fieldid WHERE cl.contextlevel = :contextlevel)';
             $moodlefields = $DB->get_records_select('user_info_field', $sql, array('contextlevel' => $ctxlvl, 'sortorder' => 'id,name'));
             $moodlefields = $moodlefields ? $moodlefields : array();
             $tmppage->param('action', 'editfield');
             $tmppage->param('from', 'moodle');
             $tmppage->param('level', 'user');
             echo '<div>';
             //popup_form("{$tmppage->url}&amp;id=",
             //           array_map(create_function('$x', 'return $x->name;'), $moodlefields),
             //           'frommoodleform', '', 'choose', '', '', false, 'self', get_string('field_from_moodle', 'local_elisprogram'));
             $actionurl = new moodle_url($tmppage->out());
             $single_select = new single_select($actionurl, 'id', array_map(create_function('$x', 'return $x->name;'), $moodlefields), null, array('' => get_string('field_from_moodle', 'local_elisprogram')));
             echo $OUTPUT->render($single_select);
             echo '</div>';
             $options = array('s' => 'field', 'action' => 'forceresync');
             $button = new single_button(new moodle_url('index.php', $options), get_string('field_force_resync', 'local_elisprogram'), 'get', array('disabled' => false, 'title' => get_string('field_force_resync', 'local_elisprogram'), 'id' => ''));
             echo $OUTPUT->render($button);
         } else {
             // create new field from scratch
             $options = array('s' => 'field', 'action' => 'editfield', 'level' => $level);
             $button = new single_button(new moodle_url('index.php', $options), get_string('field_create_new', 'local_elisprogram'), 'get', array('disabled' => false, 'title' => get_string('field_create_new', 'local_elisprogram'), 'id' => ''));
             echo $OUTPUT->render($button);
         }
     }
 }
 /**
  * Returns an SQL fragment needed to connect a context entity table to its
  * CM custom fields data in the CM system
  *
  * @param string $contextlevel Shortname of the context level we are looking for fields
  *                             related to
  * @param array $fieldids List of ids of field records we are displaying
  * @param string $instancefield Database field representing the context id
  * @return string The appropriate SQL fragment
  */
 function get_custom_field_sql($fieldids, $instancefields)
 {
     global $DB;
     $where = $DB->get_in_or_equal($fieldids);
     $contexts = $DB->get_records_select(field_contextlevel::TABLE, 'fieldid ' . $where[0], $where[1]);
     $fragment = array();
     /* *** debug ***
           ob_start();
           var_dump($instancefields);
           $tmp = ob_get_contents();
           ob_end_clean();
           error_log("UCCR::get_custom_field_sql(fieldids, instancefields = {$tmp})");
        */
     // Get the legacy context names mapped to the context level values
     $contextlevelnames = array_flip(\local_eliscore\context\helper::get_legacy_levels());
     $contextlevel = '';
     $contextname = '';
     if (!empty($contexts)) {
         //add a join for each profile field
         foreach ($contexts as $context) {
             $ctxname = $contextlevelnames[$context->contextlevel];
             if (!in_array($ctxname, array_keys($instancefields))) {
                 // Not a context level we care about
                 continue;
             }
             if ($contextlevel != $context->contextlevel) {
                 $contextlevel = $context->contextlevel;
                 $contextname = $ctxname . '_';
                 // TBD
                 list($instancefield, $_as, $_var) = explode(' ', $instancefields[$ctxname]);
                 // have one or more profile field we're joining, so join the context table at the top level
                 $fragment[$ctxname] = " LEFT JOIN {context} {$contextname}context\n                                      ON {$instancefield} = {$contextname}context.instanceid\n                                     AND {$contextname}context.contextlevel = {$contextlevel}";
             }
             $field = new field($context->fieldid);
             $identifier = "customfielddata_{$context->fieldid}";
             $fragment[] = ' LEFT JOIN {' . $field->data_table() . "} {$identifier}\n                                       ON {$contextname}context.id = {$identifier}.contextid\n                                      AND {$identifier}.fieldid = {$context->fieldid}";
         }
     }
     $result = implode("\n", $fragment);
     //         error_log("UCCR::get_custom_field_sql() => {$result}");
     return $result;
 }
Пример #3
0
 /**
  * Test validate_custom_fields function.
  */
 public function test_validatecustomfields()
 {
     // Get a form.
     $frm = new test_moodleform();
     $contextlevels = \local_eliscore\context\helper::get_legacy_levels();
     foreach ($contextlevels as $ctxname => $ctxlvl) {
         $category = $this->create_field_category($ctxlvl);
         $field = $this->create_field($category, $ctxlvl);
         switch ($ctxname) {
             case 'curriculum':
                 // Create a curriculum.
                 $cur = $this->create_curriculum($field);
                 // Set up a dummy custom field with a default value.
                 $testvalue = array('id' => $cur->id, 'field_' . $field->shortname => '');
                 break;
             case 'track':
                 // Create a track.
                 $trk = $this->create_track($cur, $field);
                 // Set up a dummy custom field with a default value.
                 $testvalue = array('id' => $trk->id, 'field_' . $field->shortname => '');
                 break;
             case 'course':
                 // Create a course.
                 $crs = $this->create_course($field);
                 // Set up a dummy custom field with a default value.
                 $testvalue = array('id' => $crs->id, 'field_' . $field->shortname => '');
                 break;
             case 'class':
                 // Create a class.
                 $cls = $this->create_class($crs, $field);
                 // Set up a dummy custom field with a default value.
                 $testvalue = array('id' => $cls->id, 'field_' . $field->shortname => '');
                 break;
             case 'user':
                 // Create a user.
                 $user = $this->create_user($field);
                 // Set up a dummy custom field with a default value.
                 $testvalue = array('id' => $user->id, 'field_' . $field->shortname => '');
                 break;
             case 'cluster':
                 // Create a userset.
                 $userset = $this->create_userset($field);
                 // Set up a dummy custom field with a default value.
                 $testvalue = array('id' => $userset->id, 'field_' . $field->shortname => '');
                 break;
         }
         $return = $frm->validate_custom_fields($testvalue, $ctxname);
         $this->assertEmpty($return);
     }
 }