function local_catalog_get_course_microcredentials($catalog_id)
{
    global $DB;
    $cm_list = $DB->get_records('local_catalog_course_mcs', array('catalog_id' => $catalog_id));
    $mc_list = local_catalog_get_all_microcredentials('id');
    $result = array();
    $sort = array();
    foreach ($cm_list as $c) {
        $result[$c->section_id]['id'] = $mc_list[$c->section_id]['id'];
        $result[$c->section_id]['name'] = $mc_list[$c->section_id]['name'];
        $result[$c->section_id]['sequence'] = $mc_list[$c->section_id]['sequence'];
        $sort[$c->section_id] = $mc_list[$c->section_id]['sequence'];
    }
    if (count($result) == 0) {
        return array();
    }
    asort($sort);
    $r = array();
    $i = 0;
    foreach ($sort as $key => $elem) {
        $r[$i] = $result[$key];
        $i++;
    }
    return $r;
}
 public function definition()
 {
     global $CFG;
     $mform = $this->_form;
     // Don't forget the underscore!
     if (isset($this->_customdata['catalog_id']) && is_object($this->_customdata['catalog_id'])) {
         $catalog_id = $this->_customdata['catalog_id'];
     }
     $mform->addElement('select', 'section_id', get_string('entry', 'local_catalog'), local_catalog_get_all_microcredentials('assoc'), array('style' => 'width: 100%'));
     $mform->addRule('section_id', get_string('required'), 'required', null, 'client');
     $this->add_action_buttons();
 }
    $data->sesskey = sesskey();
    $data->deleteicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall'));
    $data->editicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('i/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall'));
    $addform = new local_catalog_addcourse(new moodle_url($returnurl, array('action' => $addcourse)));
    $data->addform = $addform->render();
    $data->courselist = local_catalog_get_courses();
    if (count($data->courselist) > 0) {
        $data->has_courses = true;
    }
    $data->header = $OUTPUT->header();
    $data->heading = $OUTPUT->heading(get_string('coursesetup', 'local_catalog'));
    $data->footer = $OUTPUT->footer();
    echo $OUTPUT->render_from_template('local_catalog/courses_add', $data);
}
if ($displayedit) {
    local_catalog_get_all_microcredentials();
    if (isset($catalog_id)) {
        $id = $catalog_id;
    } else {
        $id = required_param('id', PARAM_INT);
    }
    $record = $DB->get_record('local_catalog', array('id' => $id), '*', MUST_EXIST);
    $editform = new local_catalog_editcourse(new moodle_url($returnurl, array('action' => $saveedits, 'id' => $id)), array('record' => $record));
    $metaform = new local_catalog_coursemeta(new moodle_url($returnurl, array('action' => $addmeta, 'catalog_id' => $id)), array('catalog_id' => $id));
    $mcform = new local_catalog_editcourse_mcs(new moodle_url($returnurl, array('action' => 'addmc', 'catalog_id' => $id)), array('catalog_id' => $id));
    $editionform = new local_catalog_course_editions(new moodle_url($returnurl, array('action' => 'addedition', 'catalog_id' => $id)), array('catalog_id' => $id));
    $addpagesform = new local_catalog_course_static_page_add(new moodle_url($returnurl, array('action' => 'addpages', 'catalog_id' => $id)), array('catalog_id' => $id));
    $data = new stdClass();
    $data->sesskey = sesskey();
    $data->deleteicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall'));
    $data->editicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('i/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall'));