~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to course/moodleform_mod.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
    /** a flag indicating whether outcomes are being used*/
50
50
    protected $_outcomesused;
51
51
 
 
52
    /**
 
53
     * @var bool A flag used to indicate that this module should lock settings
 
54
     *           based on admin settings flags in definition_after_data.
 
55
     */
 
56
    protected $applyadminlockedflags = false;
 
57
 
 
58
    /** @var object The course format of the current course. */
 
59
    protected $courseformat;
 
60
 
52
61
    function moodleform_mod($current, $section, $cm, $course) {
 
62
        global $CFG;
 
63
 
53
64
        $this->current   = $current;
54
65
        $this->_instance = $current->instance;
55
66
        $this->_section  = $section;
60
71
            $this->context = context_course::instance($course->id);
61
72
        }
62
73
 
 
74
        // Set the course format.
 
75
        require_once($CFG->dirroot . '/course/format/lib.php');
 
76
        $this->courseformat = course_get_format($course);
 
77
 
63
78
        // Guess module name
64
79
        $matches = array();
65
80
        if (!preg_match('/^mod_([^_]+)_mod_form$/', get_class($this), $matches)) {
209
224
                // is changed, maybe someone has completed it now)
210
225
                $mform->getElement('completionunlocked')->setValue(1);
211
226
            } else {
212
 
                // Has the element been unlocked?
213
 
                if ($mform->exportValue('unlockcompletion')) {
 
227
                // Has the element been unlocked, either by the button being pressed
 
228
                // in this request, or the field already being set from a previous one?
 
229
                if ($mform->exportValue('unlockcompletion') ||
 
230
                        $mform->exportValue('completionunlocked')) {
214
231
                    // Yes, add in warning text and set the hidden variable
215
232
                    $mform->insertElementBefore(
216
233
                        $mform->createElement('static', 'completedunlocked',
278
295
                }
279
296
            }
280
297
        }
 
298
 
 
299
        // Freeze admin defaults if required (and not different from default)
 
300
        $this->apply_admin_locked_flags();
281
301
    }
282
302
 
283
303
    // form verification
486
506
        if ($this->_features->groupings or $this->_features->groupmembersonly) {
487
507
            //groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
488
508
            $options = array();
489
 
            $options[0] = get_string('none');
490
509
            if ($groupings = $DB->get_records('groupings', array('courseid'=>$COURSE->id))) {
491
510
                foreach ($groupings as $grouping) {
492
511
                    $options[$grouping->id] = format_string($grouping->name);
493
512
                }
494
513
            }
 
514
            core_collator::asort($options);
 
515
            $options = array(0 => get_string('none')) + $options;
495
516
            $mform->addElement('select', 'groupingid', get_string('grouping', 'group'), $options);
496
517
            $mform->addHelpButton('groupingid', 'grouping', 'group');
497
518
        }
641
662
            $mform->addElement('hidden', 'completionunlocked', 0);
642
663
            $mform->setType('completionunlocked', PARAM_INT);
643
664
 
 
665
            $trackingdefault = COMPLETION_TRACKING_NONE;
 
666
            // If system and activity default is on, set it.
 
667
            if ($CFG->completiondefault && $this->_features->defaultcompletion) {
 
668
                $trackingdefault = COMPLETION_TRACKING_MANUAL;
 
669
            }
 
670
 
644
671
            $mform->addElement('select', 'completion', get_string('completion', 'completion'),
645
672
                array(COMPLETION_TRACKING_NONE=>get_string('completion_none', 'completion'),
646
673
                COMPLETION_TRACKING_MANUAL=>get_string('completion_manual', 'completion')));
647
 
            $mform->setDefault('completion', $this->_features->defaultcompletion
648
 
                ? COMPLETION_TRACKING_MANUAL
649
 
                : COMPLETION_TRACKING_NONE);
 
674
            $mform->setDefault('completion', $trackingdefault);
650
675
            $mform->addHelpButton('completion', 'completion', 'completion');
651
676
 
652
677
            // Automatic completion once you view it
665
690
                $mform->disabledIf('completionusegrade', 'completion', 'ne', COMPLETION_TRACKING_AUTOMATIC);
666
691
                $mform->addHelpButton('completionusegrade', 'completionusegrade', 'completion');
667
692
                $gotcompletionoptions = true;
 
693
 
 
694
                // If using the rating system, there is no grade unless ratings are enabled.
 
695
                if ($this->_features->rating) {
 
696
                    $mform->disabledIf('completionusegrade', 'assessed', 'eq', 0);
 
697
                }
668
698
            }
669
699
 
670
700
            // Automatic completion according to module-specific rules
812
842
        $label = is_null($customlabel) ? get_string('moduleintro') : $customlabel;
813
843
 
814
844
        $mform->addElement('editor', 'introeditor', $label, array('rows' => 10), array('maxfiles' => EDITOR_UNLIMITED_FILES,
815
 
            'noclean' => true, 'context' => $this->context, 'collapsed' => true));
 
845
            'noclean' => true, 'context' => $this->context, 'subdirs' => true));
816
846
        $mform->setType('introeditor', PARAM_RAW); // no XSS prevention here, users must be trusted
817
847
        if ($required) {
818
848
            $mform->addRule('introeditor', get_string('required'), 'required', null, 'client');
819
849
        }
820
850
 
821
 
        // If the 'show description' feature is enabled, this checkbox appears
822
 
        // below the intro.
823
 
        if ($this->_features->showdescription) {
 
851
        // If the 'show description' feature is enabled, this checkbox appears below the intro.
 
852
        // We want to hide that when using the singleactivity course format because it is confusing.
 
853
        if ($this->_features->showdescription  && $this->courseformat->has_view_page()) {
824
854
            $mform->addElement('checkbox', 'showdescription', get_string('showdescription'));
825
855
            $mform->addHelpButton('showdescription', 'showdescription');
826
856
        }
848
878
        // elements in a row need a group
849
879
        $buttonarray = array();
850
880
 
851
 
        if ($submit2label !== false) {
 
881
        // Label for the submit button to return to the course.
 
882
        // Ignore this button in single activity format because it is confusing.
 
883
        if ($submit2label !== false && $this->courseformat->has_view_page()) {
852
884
            $buttonarray[] = &$mform->createElement('submit', 'submitbutton2', $submit2label);
853
885
        }
854
886
 
864
896
        $mform->setType('buttonar', PARAM_RAW);
865
897
        $mform->closeHeaderBefore('buttonar');
866
898
    }
 
899
 
 
900
    /**
 
901
     * Get the list of admin settings for this module and apply any locked settings.
 
902
     * This cannot happen in apply_admin_defaults because we do not the current values of the settings
 
903
     * in that function because set_data has not been called yet.
 
904
     *
 
905
     * @return void
 
906
     */
 
907
    protected function apply_admin_locked_flags() {
 
908
        global $OUTPUT;
 
909
 
 
910
        if (!$this->applyadminlockedflags) {
 
911
            return;
 
912
        }
 
913
 
 
914
        $settings = get_config($this->_modname);
 
915
        $mform = $this->_form;
 
916
        $lockedicon = html_writer::tag('span',
 
917
                                       $OUTPUT->pix_icon('t/locked', get_string('locked', 'admin')),
 
918
                                       array('class' => 'action-icon'));
 
919
        $isupdate = !empty($this->_cm);
 
920
 
 
921
        foreach ($settings as $name => $value) {
 
922
            if (strpos('_', $name) !== false) {
 
923
                continue;
 
924
            }
 
925
            if ($mform->elementExists($name)) {
 
926
                $element = $mform->getElement($name);
 
927
                $lockedsetting = $name . '_locked';
 
928
                if (!empty($settings->$lockedsetting)) {
 
929
                    // Always lock locked settings for new modules,
 
930
                    // for updates, only lock them if the current value is the same as the default (or there is no current value).
 
931
                    $value = $settings->$name;
 
932
                    if ($isupdate && isset($this->current->$name)) {
 
933
                        $value = $this->current->$name;
 
934
                    }
 
935
                    if ($value == $settings->$name) {
 
936
                        $mform->setConstant($name, $settings->$name);
 
937
                        $element->setLabel($element->getLabel() . $lockedicon);
 
938
                        // Do not use hardfreeze because we need the hidden input to check dependencies.
 
939
                        $element->freeze();
 
940
                    }
 
941
                }
 
942
            }
 
943
        }
 
944
    }
 
945
 
 
946
    /**
 
947
     * Get the list of admin settings for this module and apply any defaults/advanced/locked settings.
 
948
     *
 
949
     * @param $datetimeoffsets array - If passed, this is an array of fieldnames => times that the
 
950
     *                         default date/time value should be relative to. If not passed, all
 
951
     *                         date/time fields are set relative to the users current midnight.
 
952
     * @return void
 
953
     */
 
954
    public function apply_admin_defaults($datetimeoffsets = array()) {
 
955
        // This flag triggers the settings to be locked in apply_admin_locked_flags().
 
956
        $this->applyadminlockedflags = true;
 
957
 
 
958
        $settings = get_config($this->_modname);
 
959
        $mform = $this->_form;
 
960
        $usermidnight = usergetmidnight(time());
 
961
        $isupdate = !empty($this->_cm);
 
962
 
 
963
        foreach ($settings as $name => $value) {
 
964
            if (strpos('_', $name) !== false) {
 
965
                continue;
 
966
            }
 
967
            if ($mform->elementExists($name)) {
 
968
                $element = $mform->getElement($name);
 
969
                if (!$isupdate) {
 
970
                    if ($element->getType() == 'date_time_selector') {
 
971
                        $enabledsetting = $name . '_enabled';
 
972
                        if (empty($settings->$enabledsetting)) {
 
973
                            $mform->setDefault($name, 0);
 
974
                        } else {
 
975
                            $relativetime = $usermidnight;
 
976
                            if (isset($datetimeoffsets[$name])) {
 
977
                                $relativetime = $datetimeoffsets[$name];
 
978
                            }
 
979
                            $mform->setDefault($name, $relativetime + $settings->$name);
 
980
                        }
 
981
                    } else {
 
982
                        $mform->setDefault($name, $settings->$name);
 
983
                    }
 
984
                }
 
985
                $advancedsetting = $name . '_adv';
 
986
                if (!empty($settings->$advancedsetting)) {
 
987
                    $mform->setAdvanced($name);
 
988
                }
 
989
            }
 
990
        }
 
991
    }
867
992
}
868
993
 
869
994