How can i get the course and teacher name ?

How can i get the course and teacher name ?

by Ivan Torres -
Number of replies: 6

Hi friends...


I have the next problem...

In the front end i have the avaible course, each one with the name of courses and teacher's name.

My problem is when i click it in one course i need to get the name of course and the teacher and put this information in the header of moodle.

I have the course name but i can't get the name of teacher.


In the header i put this query 

    $nombrecurso = $PAGE->course->fullname;

    $teacher_name = $DB->get_recordset_sql("SELECT ldm_user.id, ldm_course.newsitems, ldm_user.firstname, ldm_user.lastname,        ldm_course.shortname, ldm_course.fullname

    FROM ldm_user, ldm_course WHERE ldm_course.fullname = '$nombrecurso'");      

    foreach($teacher_name as $teacher){

    echo "Asesor(a):". $teacher->firstname; echo " ".$teacher->lastname ; 

    };

I'm using moodle 2.7 

Somebody can help me please... 


Attachment pantalla_1.jpg
Attachment pantalla_2.jpg
Average of ratings: -
In reply to Ivan Torres

Re: How can i get the course and teacher name ?

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

What you are actually asking is, "who is assigned the role of 'teacher' in the course context for course xyz?"

So, if you have the course id number, you first need

$context = course_context::instance($courseid);

then...

$users = get_enrolled_users($context)

(there may be more than one). Check lib/accesslib.php for the full definition. 

All this is from memory (so check). To generalise this a bit more, you might also want to look into 'course contacts'.

Average of ratings: Useful (2)
In reply to Howard Miller

Re: How can i get the course and teacher name ?

by Ivan Torres -

Just i want show the course name and teacher name, example...

If you are in frontpage and click it in the course 1 (image click )

In the header of the course page show the name of the course (course 1) and show the teacher the course (Image show).


Attachment click.jpg
Attachment show.jpg
In reply to Ivan Torres

Re: How can i get the course and teacher name ?

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

OK...

- what else do you need to know?

- what do you plan to do if the course has more than one teacher?

In reply to Howard Miller

Re: How can i get the course and teacher name ?

by Ivan Torres -
This courses no have more that one teacher, every course just have one teacher.... I know how to get the name of courses $page->course->shortname, but i dont know how to get the teacher name...


Thanks for your help, meabe its a stupid question but i am new in moodle and i dont know how to do it... 

Thanks again 


In reply to Ivan Torres

Re: How can i get the course and teacher name ?

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I just told you above. If you have the course object then you have the id ($course->id probably). From that you can get the course context and from that you can get the people enrolled with the teacher role. You may only have one teacher but Moodle can have many, so the function will return a list of user objects. To get the name do something like...

if ($users) {
    $teachername = fullname($users[0]);
}
In reply to Howard Miller

Re: How can i get the course and teacher name ?

by Ivan Torres -

Thanks Howard Miller...

I have another question....

How is posible that in module Avaible course can get the course name and the correspond teacher?? (image 1)

This method can be used to show it in the header of the course?


 $coursename = $chelper->get_course_formatted_name($course);

        $coursenamelink = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)),

                                            $coursename, array('class' => $course->visible ? '' : 'dimmed'));

        $content .= html_writer::tag($nametag, $coursenamelink, array('class' => 'coursename'));

        // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page.

        $content .= html_writer::start_tag('div', array('class' => 'moreinfo'));

        if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) {

            if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles()) {

                $url = new moodle_url('/course/info.php', array('id' => $course->id));

                $image = html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/info'),

                    'alt' => $this->strings->summary));

                $content .= html_writer::link($url, $image, array('title' => $this->strings->summary));

                // Make sure JS file to expand course content is included.

                $this->coursecat_include_js();

            }

        }

        $content .= html_writer::end_tag('div'); // .moreinfo


Thanks again Howard Miller...

Attachment click.jpg
Average of ratings: Useful (1)