Displaying a custom course setting in the Clean theme?

Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-
Number of replies: 15

Hello.

On my local Moodle 2.8 server I have created a custom field called "credits" when adding a new course. Adding this field allows teachers to put in the amount of credits the course offers, but it won't do much good if the students can't view it somehow. Would it be possible to display my custom credits field on the actual course page using the Clean theme version 2014110400? I know changing the Moodle core is ill-advised, but we intend to stick to version 2.8 due to the numerous other changes we've made.

དཔྱ་སྙོམས་ཀྱི་སྐུགས་ཚུ།: -
In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Mary Evans གིས-

Hello 

Can you explain how you added a custom field to a course. I know how to add a custom filed for a Student (User) but never encountered a setting in a course!

EDITED:

I have just been thinking about this. Did you mean you added a new custom field in the Gradebook for a new course?

Cheers

Mary

In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Mary Evans གིས-

Hi,

Further to my earlier comment. I have just been testing this and if you add a custom field to a Gradebook, such as 'Credits' a student can see this in his or her Gradebook if they are allowed to see their grades that is?

https://docs.moodle.org/28/en/Grade_items#Grade_items_and_gradebook

Cheers

Mary

In reply to Mary Evans

Re: Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-

Hi Mary.

Perhaps this image will help explain what I meant when I said I added a credits field to course settings.


Basically, we want to display this field on the course page so that students can view the amount of credits before they decide if they want to take the course (we will be using guest access). 


To create this field, all I did was add the following code to moodle\course\edit_form.php

  $mform->addElement('text', 'credits', 'UE Credits', 'maxlength="20" size="1"');

        $mform->addRule('credits', 'Missing credits', 'required', null, 'client');

        $mform->setType('credits', PARAM_INT);


Hopefully that clears things up.

In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Mary Evans གིས-

Ah...I see.

OK then, does this extra form item show up anywhere other than the course form for setting up a course?

This is, I suspect something that might be easier adding into a Course Format which is Gareth's speciality. That way you can tailor make a course to fit your needs.

Just as a matter of interest, which Course Formal are you using at present for these courses?

It may be possible to use a renderer and extend it via your theme, but I am cluching at straws here.

If it were me I would be adding this Credit directly into the Course Summary as this is linked to Courses displayed on the Frontpage using the Course Combo option.

Cheers

Mary

In reply to Mary Evans

Re: Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-

No, currently this item doesn't show up anywhere else. We intend on using the Topics format for nearly all of our courses. 

Adding the credits to the course summary is an idea we've thought of before. We aren't necessarily opposed to the idea, but we can't force teachers to put the amount of credits there. That is why I wanted to make credits a required field and attempt to display it that way. 

Do you think you could elaborate on extending the renderer via the theme? I wouldn't mind giving that a shot. 

In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Gareth J Barnard གིས-
Core developers གི་པར Particularly helpful Moodlers གི་པར Plugin developers གི་པར

Don't modify the form code.  Clone 'Topics' and then use 'course format options' in lib.php.  Read: https://docs.moodle.org/dev/Course_formats and look at other course formats to learn how to do this.

In reply to Gareth J Barnard

Re: Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-

Are you suggesting to get rid of the credits field entirely and to just add it to the 'course format options' method in lib.php? Getting rid of the credits field is no problem, but I've been having trouble with adding it to the 'course format options' method due to being unfamiliar with php. I've added the following code, but selecting the 'clone' format gives me a "page isn't working" error". I'm guessing the error is caused by the first if statement? Not entirely sure if what I have would display the credits either. Oh and since Richard was curious, I did add the credits field to my database.

 static $courseformatoptions = false;
        if ($courseformatoptions === false) {
'coursecredits' => array(
                    'default' => $course->credits,
                    'type' => PARAM_INT,
                ),
}
 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
)
                 'coursecredits' => array(
                    'label' => new lang_string('coursecredits'),
                    'element_type' => 'select',
                    'element_attributes' => array(
                        array(
                            1,
                            2,
                            3)
                        )
                ),
            );
            $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
        }
        return $courseformatoptions;
In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Gareth J Barnard གིས-
Core developers གི་པར Particularly helpful Moodlers གི་པར Plugin developers གི་པར

RE: Are you suggesting to get rid of the credits field entirely and to just add it to the 'course format options' method in lib.php? - yes as it does diddly squat except add a UI component. 

2. Don't add the field manually to the database, the code in lib.php does that = probably the cause of the problem.

3. You've defined a core language string and not one pertaining to the new format.

4. The default setting looks wrong.

Look at how Collapsed Topics / Grid format / Columns format does this.

In reply to Gareth J Barnard

Re: Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-
I've looked at the code for the Collapsed Topics / Grid format/ Column format plugins, and they helped a lot. So thank you for recommending that I take a look at those. I'm still getting the "page display error" however. I've removed the 'credits' field from the course creation page, and I've changed the code to the following:


public function course_format_options($foreditform = false) {
        static $courseformatoptions = false;
        if ($courseformatoptions === false) {
 'coursecredits' => array(
                    'default' => get_config('format_credits', 'defaultcredits');
                    'type' => PARAM_INT,
                ),
            );
        }
 if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
 if (has_capability('format/credits:changecredits', $coursecontext)) {
$courseformatoptionsedit['credits'] = array(
                    'label' => new lang_string('setcredits', 'format_credits'),
                    'element_type' => 'select',
                    'element_attributes' => array(
                        array(1 => get_string('one', 'format_credits')
                            2 => get_string('two', 'format_credits'),
                            3 => get_string('three', 'format_credits'))
                        );
                )
            }
            $courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
        }
        return $courseformatoptions;
    }


Any idea of what could have gone wrong? I'm stumped. It might be worth mentioning that I haven't touched the 'create_edit_form_elements' function yet. Perhaps that is the source of the problem?

In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Gareth J Barnard གིས-
Core developers གི་པར Particularly helpful Moodlers གི་པར Plugin developers གི་པར

Whats the full stack trace of the error after you have turned developer level debugging on?

You don't need to touch 'create_edit_form_elements' - I do in CT and Grid for the reset functionality, but you don't.

In reply to Gareth J Barnard

Re: Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-

This is what I'm getting with Developer debugging turned on:

Debug info: 
Error code: needcoursecategroyid
Stack trace:
line 481 of \lib\setuplib.php: moodle_exception thrown
line 69 of \course\edit.php: call to print_error()
In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Gareth J Barnard གིས-
Core developers གི་པར Particularly helpful Moodlers གི་པར Plugin developers གི་པར

Odd. I don't know.  Must be something with the code not published here.  Think about what you have done and what might be causing it.

In reply to Gareth J Barnard

Re: Displaying a custom course setting in the Clean theme?

Tevin Jemison གིས-

I commented out the code I posted and it works without errors, so the problem definitely seems to be with the code in the previous posts/ something it references. 

Last question! Could you explain the parameters of the get_config and get_string functions (or just point me towards the appropriate api)? 

In reply to Tevin Jemison

Re: Displaying a custom course setting in the Clean theme?

Richard Oelmann གིས-
Core developers གི་པར Plugin developers གི་པར Testers གི་པར

If you are storing the entry correctly then you should be able to access it through $course->credits, I think.

But this is where we probably need Gareth's input to check this out as I'm not 100% sure of this - I don't think simply adding the item to the form is enough. If there's no changes to the database to store that information in (an additional field in the mdl_course table), then I don't think the value is being stored anywhere. I don't believe that just creating the additional form item would automatically create the field in the database and I think (but could be wrong) that course information like this is stored in the database rather than in a cache file somewhere.

EDIT: Ah cross posted with Gareth - recommend Gareth's solution as he is the course format guru དགའ་འཛུམ་