Esempio n. 1
0
 /**
  * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
  */
 function definition_after_data()
 {
     global $CFG, $COURSE;
     $mform =& $this->_form;
     if ($id = $mform->getElementValue('update')) {
         $modulename = $mform->getElementValue('modulename');
         $instance = $mform->getElementValue('instance');
         if ($this->_features->gradecat) {
             $gradecat = false;
             if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
                 $outcomes = grade_outcome::fetch_all_available($COURSE->id);
                 if (!empty($outcomes)) {
                     $gradecat = true;
                 }
             }
             $hasgradeitems = false;
             $items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $instance, 'courseid' => $COURSE->id));
             //will be no items if, for example, this activity supports ratings but rating aggregate type == no ratings
             if (!empty($items)) {
                 foreach ($items as $item) {
                     if (!empty($item->outcomeid)) {
                         $elname = 'outcome_' . $item->outcomeid;
                         if ($mform->elementExists($elname)) {
                             $mform->hardFreeze($elname);
                             // prevent removing of existing outcomes
                         }
                     } else {
                         $hasgradeitems = true;
                     }
                 }
                 foreach ($items as $item) {
                     if (is_bool($gradecat)) {
                         $gradecat = $item->categoryid;
                         continue;
                     }
                     if ($gradecat != $item->categoryid) {
                         //mixed categories
                         $gradecat = false;
                         break;
                     }
                 }
             }
             if (!$hasgradeitems && $mform->elementExists('gradepass')) {
                 // Remove form element 'Grade to pass' since there are no grade items (when rating not selected).
                 $mform->removeElement('gradepass');
             }
             if ($gradecat === false) {
                 // items and outcomes in different categories - remove the option
                 // TODO: add a "Mixed categories" text instead of removing elements with no explanation
                 if ($mform->elementExists('gradecat')) {
                     $mform->removeElement('gradecat');
                     if ($this->_features->rating && !$mform->elementExists('gradepass')) {
                         //if supports ratings then the max grade dropdown wasnt added so the grade box can be removed entirely
                         $mform->removeElement('modstandardgrade');
                     }
                 }
             }
         }
     }
     if ($COURSE->groupmodeforce) {
         if ($mform->elementExists('groupmode')) {
             $mform->hardFreeze('groupmode');
             // groupmode can not be changed if forced from course settings
         }
     }
     // Don't disable/remove groupingid if it is currently set to something,
     // otherwise you cannot turn it off at same time as turning off other
     // option (MDL-30764)
     if (empty($this->_cm) || !$this->_cm->groupingid) {
         if ($mform->elementExists('groupmode') && empty($COURSE->groupmodeforce)) {
             $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
         } else {
             if (!$mform->elementExists('groupmode')) {
                 // Groupings have no use without groupmode.
                 if ($mform->elementExists('groupingid')) {
                     $mform->removeElement('groupingid');
                 }
             }
         }
     }
     // Completion: If necessary, freeze fields
     $completion = new completion_info($COURSE);
     if ($completion->is_enabled()) {
         // If anybody has completed the activity, these options will be 'locked'
         $completedcount = empty($this->_cm) ? 0 : $completion->count_user_data($this->_cm);
         $freeze = false;
         if (!$completedcount) {
             if ($mform->elementExists('unlockcompletion')) {
                 $mform->removeElement('unlockcompletion');
             }
             // Automatically set to unlocked (note: this is necessary
             // in order to make it recalculate completion once the option
             // is changed, maybe someone has completed it now)
             $mform->getElement('completionunlocked')->setValue(1);
         } else {
             // Has the element been unlocked, either by the button being pressed
             // in this request, or the field already being set from a previous one?
             if ($mform->exportValue('unlockcompletion') || $mform->exportValue('completionunlocked')) {
                 // Yes, add in warning text and set the hidden variable
                 $mform->insertElementBefore($mform->createElement('static', 'completedunlocked', get_string('completedunlocked', 'completion'), get_string('completedunlockedtext', 'completion')), 'unlockcompletion');
                 $mform->removeElement('unlockcompletion');
                 $mform->getElement('completionunlocked')->setValue(1);
             } else {
                 // No, add in the warning text with the count (now we know
                 // it) before the unlock button
                 $mform->insertElementBefore($mform->createElement('static', 'completedwarning', get_string('completedwarning', 'completion'), get_string('completedwarningtext', 'completion', $completedcount)), 'unlockcompletion');
                 $freeze = true;
             }
         }
         if ($freeze) {
             $mform->freeze('completion');
             if ($mform->elementExists('completionview')) {
                 $mform->freeze('completionview');
                 // don't use hardFreeze or checkbox value gets lost
             }
             if ($mform->elementExists('completionusegrade')) {
                 $mform->freeze('completionusegrade');
             }
             $mform->freeze($this->_customcompletionelements);
         }
     }
     // Freeze admin defaults if required (and not different from default)
     $this->apply_admin_locked_flags();
 }
 public function test_count_user_data()
 {
     global $DB;
     $this->mock_setup();
     $course = (object) array('id' => 13);
     $cm = (object) array('id' => 42);
     /** @var $DB PHPUnit_Framework_MockObject_MockObject */
     $DB->expects($this->at(0))->method('get_field_sql')->will($this->returnValue(666));
     $c = new completion_info($course);
     $this->assertEquals(666, $c->count_user_data($cm));
 }
Esempio n. 3
0
 /**
  * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
  */
 function definition_after_data()
 {
     global $CFG, $COURSE;
     $mform =& $this->_form;
     if ($id = $mform->getElementValue('update')) {
         $modulename = $mform->getElementValue('modulename');
         $instance = $mform->getElementValue('instance');
         if ($this->_features->gradecat) {
             $gradecat = false;
             if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
                 $outcomes = grade_outcome::fetch_all_available($COURSE->id);
                 if (!empty($outcomes)) {
                     $gradecat = true;
                 }
             }
             $items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $instance, 'courseid' => $COURSE->id));
             //will be no items if, for example, this activity supports ratings but rating aggregate type == no ratings
             if (!empty($items)) {
                 foreach ($items as $item) {
                     if (!empty($item->outcomeid)) {
                         $elname = 'outcome_' . $item->outcomeid;
                         if ($mform->elementExists($elname)) {
                             $mform->hardFreeze($elname);
                             // prevent removing of existing outcomes
                         }
                     }
                 }
                 foreach ($items as $item) {
                     if (is_bool($gradecat)) {
                         $gradecat = $item->categoryid;
                         continue;
                     }
                     if ($gradecat != $item->categoryid) {
                         //mixed categories
                         $gradecat = false;
                         break;
                     }
                 }
             }
             if ($gradecat === false) {
                 // items and outcomes in different categories - remove the option
                 // TODO: add a "Mixed categories" text instead of removing elements with no explanation
                 if ($mform->elementExists('gradecat')) {
                     $mform->removeElement('gradecat');
                     if ($this->_features->rating) {
                         //if supports ratings then the max grade dropdown wasnt added so the grade box can be removed entirely
                         $mform->removeElement('modstandardgrade');
                     }
                 }
             }
         }
     }
     if ($COURSE->groupmodeforce) {
         if ($mform->elementExists('groupmode')) {
             $mform->hardFreeze('groupmode');
             // groupmode can not be changed if forced from course settings
         }
     }
     // Don't disable/remove groupingid if it is currently set to something,
     // otherwise you cannot turn it off at same time as turning off other
     // option (MDL-30764)
     if (empty($this->_cm) || !$this->_cm->groupingid) {
         if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
             $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
         } else {
             if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
                 $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
             } else {
                 if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
                     // groupings have no use without groupmode or groupmembersonly
                     if ($mform->elementExists('groupingid')) {
                         $mform->removeElement('groupingid');
                     }
                 }
             }
         }
     }
     // Completion: If necessary, freeze fields
     $completion = new completion_info($COURSE);
     if ($completion->is_enabled()) {
         // If anybody has completed the activity, these options will be 'locked'
         $completedcount = empty($this->_cm) ? 0 : $completion->count_user_data($this->_cm);
         $freeze = false;
         if (!$completedcount) {
             if ($mform->elementExists('unlockcompletion')) {
                 $mform->removeElement('unlockcompletion');
             }
             // Automatically set to unlocked (note: this is necessary
             // in order to make it recalculate completion once the option
             // is changed, maybe someone has completed it now)
             $mform->getElement('completionunlocked')->setValue(1);
         } else {
             // Has the element been unlocked?
             if ($mform->exportValue('unlockcompletion')) {
                 // Yes, add in warning text and set the hidden variable
                 $mform->insertElementBefore($mform->createElement('static', 'completedunlocked', get_string('completedunlocked', 'completion'), get_string('completedunlockedtext', 'completion')), 'unlockcompletion');
                 $mform->removeElement('unlockcompletion');
                 $mform->getElement('completionunlocked')->setValue(1);
             } else {
                 // No, add in the warning text with the count (now we know
                 // it) before the unlock button
                 $mform->insertElementBefore($mform->createElement('static', 'completedwarning', get_string('completedwarning', 'completion'), get_string('completedwarningtext', 'completion', $completedcount)), 'unlockcompletion');
                 $freeze = true;
             }
         }
         if ($freeze) {
             $mform->freeze('completion');
             if ($mform->elementExists('completionview')) {
                 $mform->freeze('completionview');
                 // don't use hardFreeze or checkbox value gets lost
             }
             if ($mform->elementExists('completionusegrade')) {
                 $mform->freeze('completionusegrade');
             }
             $mform->freeze($this->_customcompletionelements);
         }
     }
     // Availability conditions
     if (!empty($CFG->enableavailability) && $this->_cm) {
         $ci = new condition_info($this->_cm);
         $fullcm = $ci->get_full_course_module();
         $num = 0;
         foreach ($fullcm->conditionsgrade as $gradeitemid => $minmax) {
             $groupelements = $mform->getElement('conditiongradegroup[' . $num . ']')->getElements();
             $groupelements[0]->setValue($gradeitemid);
             $groupelements[2]->setValue(is_null($minmax->min) ? '' : format_float($minmax->min, 5, true, true));
             $groupelements[4]->setValue(is_null($minmax->max) ? '' : format_float($minmax->max, 5, true, true));
             $num++;
         }
         $num = 0;
         foreach ($fullcm->conditionsfield as $field => $details) {
             $groupelements = $mform->getElement('conditionfieldgroup[' . $num . ']')->getElements();
             $groupelements[0]->setValue($field);
             $groupelements[1]->setValue(is_null($details->operator) ? '' : $details->operator);
             $groupelements[2]->setValue(is_null($details->value) ? '' : $details->value);
             $num++;
         }
         if ($completion->is_enabled()) {
             $num = 0;
             foreach ($fullcm->conditionscompletion as $othercmid => $state) {
                 $groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
                 $groupelements[0]->setValue($othercmid);
                 $groupelements[1]->setValue($state);
                 $num++;
             }
         }
     }
 }
Esempio n. 4
0
    function test_count_user_data() {
        global $DB;

        $course = (object)array('id'=>13);
        $cm = (object)array('id'=>42);

        /** @var $DB PHPUnit_Framework_MockObject_MockObject */
        $DB->expects($this->at(0))
            ->method('get_field_sql')
            ->will($this->returnValue(666));

/*
        $DB->expectOnce('get_field_sql',array(new IgnoreWhitespaceExpectation("SELECT
    COUNT(1)
FROM
    {course_modules_completion}
WHERE
    coursemoduleid=? AND completionstate<>0"),array(42)));
*/

        $c = new completion_info($course);
        $this->assertEquals(666, $c->count_user_data($cm));
    }
Esempio n. 5
0
 function test_count_user_data()
 {
     global $DB;
     $course = (object) array('id' => 13);
     $cm = (object) array('id' => 42);
     $DB->setReturnValue('get_field_sql', 666);
     $DB->expectOnce('get_field_sql', array(new IgnoreWhitespaceExpectation("SELECT\n    COUNT(1)\nFROM\n    {course_modules_completion}\nWHERE\n    coursemoduleid=? AND completionstate<>0"), array(42)));
     $c = new completion_info($course);
     $this->assertEqual(666, $c->count_user_data($cm));
     $DB->tally();
 }
Esempio n. 6
0
 /**
  * Each module which defines definition_after_data() must call this method using parent::definition_after_data();
  */
 function definition_after_data()
 {
     global $CFG, $COURSE;
     $mform =& $this->_form;
     if ($id = $mform->getElementValue('update')) {
         $modulename = $mform->getElementValue('modulename');
         $instance = $mform->getElementValue('instance');
         if ($this->_features->gradecat) {
             $gradecat = false;
             if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
                 if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
                     $gradecat = true;
                 }
             }
             if ($items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $instance, 'courseid' => $COURSE->id))) {
                 foreach ($items as $item) {
                     if (!empty($item->outcomeid)) {
                         $elname = 'outcome_' . $item->outcomeid;
                         if ($mform->elementExists($elname)) {
                             $mform->hardFreeze($elname);
                             // prevent removing of existing outcomes
                         }
                     }
                 }
                 foreach ($items as $item) {
                     if (is_bool($gradecat)) {
                         $gradecat = $item->categoryid;
                         continue;
                     }
                     if ($gradecat != $item->categoryid) {
                         //mixed categories
                         $gradecat = false;
                         break;
                     }
                 }
             }
             if ($gradecat === false) {
                 // items and outcomes in different categories - remove the option
                 // TODO: it might be better to add a "Mixed categories" text instead
                 if ($mform->elementExists('gradecat')) {
                     $mform->removeElement('gradecat');
                 }
             }
         }
     }
     if ($COURSE->groupmodeforce) {
         if ($mform->elementExists('groupmode')) {
             $mform->hardFreeze('groupmode');
             // groupmode can not be changed if forced from course settings
         }
     }
     if ($mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly') and empty($COURSE->groupmodeforce)) {
         $mform->disabledIf('groupingid', 'groupmode', 'eq', NOGROUPS);
     } else {
         if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
             $mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
         } else {
             if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
                 // groupings have no use without groupmode or groupmembersonly
                 if ($mform->elementExists('groupingid')) {
                     $mform->removeElement('groupingid');
                 }
             }
         }
     }
     // Completion: If necessary, freeze fields
     $completion = new completion_info($COURSE);
     if ($completion->is_enabled()) {
         // If anybody has completed the activity, these options will be 'locked'
         $completedcount = empty($this->_cm) ? 0 : $completion->count_user_data($this->_cm);
         $freeze = false;
         if (!$completedcount) {
             if ($mform->elementExists('unlockcompletion')) {
                 $mform->removeElement('unlockcompletion');
             }
         } else {
             // Has the element been unlocked?
             if ($mform->exportValue('unlockcompletion')) {
                 // Yes, add in warning text and set the hidden variable
                 $mform->insertElementBefore($mform->createElement('static', 'completedunlocked', get_string('completedunlocked', 'completion'), get_string('completedunlockedtext', 'completion')), 'unlockcompletion');
                 $mform->removeElement('unlockcompletion');
                 $mform->getElement('completionunlocked')->setValue(1);
             } else {
                 // No, add in the warning text with the count (now we know
                 // it) before the unlock button
                 $mform->insertElementBefore($mform->createElement('static', 'completedwarning', get_string('completedwarning', 'completion'), get_string('completedwarningtext', 'completion', $completedcount)), 'unlockcompletion');
                 $mform->setHelpButton('completedwarning', array('completionlocked', get_string('help_completionlocked', 'completion'), 'completion'));
                 $freeze = true;
             }
         }
         if ($freeze) {
             $mform->freeze('completion');
             if ($mform->elementExists('completionview')) {
                 $mform->freeze('completionview');
                 // don't use hardFreeze or checkbox value gets lost
             }
             if ($mform->elementExists('completionusegrade')) {
                 $mform->freeze('completionusegrade');
             }
             $mform->freeze($this->_customcompletionelements);
         }
     }
     // Availability conditions
     if (!empty($CFG->enableavailability) && $this->_cm) {
         $ci = new condition_info($this->_cm);
         $fullcm = $ci->get_full_course_module();
         $num = 0;
         foreach ($fullcm->conditionsgrade as $gradeitemid => $minmax) {
             $groupelements = $mform->getElement('conditiongradegroup[' . $num . ']')->getElements();
             $groupelements[0]->setValue($gradeitemid);
             // These numbers are always in the format 0.00000 - the rtrims remove any final zeros and,
             // if it is a whole number, the decimal place.
             $groupelements[2]->setValue(is_null($minmax->min) ? '' : rtrim(rtrim($minmax->min, '0'), '.'));
             $groupelements[4]->setValue(is_null($minmax->max) ? '' : rtrim(rtrim($minmax->max, '0'), '.'));
             $num++;
         }
         if ($completion->is_enabled()) {
             $num = 0;
             foreach ($fullcm->conditionscompletion as $othercmid => $state) {
                 $groupelements = $mform->getElement('conditioncompletiongroup[' . $num . ']')->getElements();
                 $groupelements[0]->setValue($othercmid);
                 $groupelements[1]->setValue($state);
                 $num++;
             }
         }
     }
 }