/** * @see lib/moodleform#definition() */ public function definition() { global $CFG, $COURSE, $DB, $PAGE; $mform = $this->_form; $modinfo = $this->_customdata['modinfo']; $course = $this->_customdata['course']; $activitytype = $this->_customdata['activitytype']; $coursehasavailability = !empty($CFG->enableavailability); $coursehascompletion = !empty($CFG->enablecompletion) && !empty($course->enablecompletion); // Context instance of the course. $coursecontext = context_course::instance($course->id); // Store current activity type. $mform->addElement('hidden', 'activitytype', $activitytype); $mform->setType('activitytype', PARAM_PLUGIN); // Add action button to the top of the form. $addactionbuttons = false; $this->add_action_buttons(); // Course start date. $mform->addElement('header', 'coursestartdateheader', get_string('coursestartdateheader', 'report_editdates')); $mform->setExpanded('coursestartdateheader', false); $mform->addElement('date_selector', 'coursestartdate', get_string('startdate')); $mform->addHelpButton('coursestartdate', 'startdate'); $mform->setDefault('coursestartdate', $course->startdate); // If user is not capable, make it read only. if (!has_capability('moodle/course:update', $coursecontext)) { $mform->hardFreeze('coursestartdate'); } else { $addactionbuttons = true; } // Var to count the number of elements in the course/sections. // It will be used to decide whether to show save action button // at the bottom of the form page. $elementadded = 0; // Default -1 to display header for 0th section. $prevsectionnum = -1; // Cycle through all the sections in the course. $cms = $modinfo->get_cms(); $sections = $modinfo->get_section_info_all(); foreach ($sections as $sectionnum => $section) { $ismodadded = false; $sectionname = ''; // Skip if section isn't visible to the user. if (!$section->uservisible) { continue; } // New section, create header. if ($prevsectionnum != $sectionnum) { $sectionname = get_section_name($course, $section); $headername = 'section' . $sectionnum . 'header'; $mform->addElement('header', $headername, $sectionname); $mform->setExpanded($headername, false); $prevsectionnum = $sectionnum; } if ($coursehasavailability && $section->availability) { // If there are retricted access date settings. if (strpos($section->availability, '"type":"date"') !== false) { $editsettingurl = new moodle_url('/course/editsection.php', array('id' => $section->id)); $editsettingurltext = html_writer::tag('a', get_string('editrestrictedaccess', 'report_editdates'), array('href' => $editsettingurl->out(false), 'target' => '_blank')); $mform->addElement('static', '', get_string('sectionhasrestrictedaccess', 'report_editdates', get_section_name($course, $section)), $editsettingurltext); } } // Cycle through each module in a section. if (isset($modinfo->sections[$sectionnum])) { foreach ($modinfo->sections[$sectionnum] as $cmid) { $cm = $cms[$cmid]; // No need to display/continue if this module is not visible to user. if (!$cm->uservisible) { continue; } // If activity filter is on, then filter module by activity type. if ($activitytype && $cm->modname != $activitytype) { continue; } // Check if the user has capability to edit this module settings. $modulecontext = context_module::instance($cm->id); $ismodreadonly = !has_capability('moodle/course:manageactivities', $modulecontext); // Display activity name. $iconmarkup = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(), 'class' => 'activityicon', 'alt' => '')); $stractivityname = html_writer::tag('strong', $iconmarkup . $cm->name); $mform->addElement('static', 'modname' . $cm->id, $stractivityname); $isdateadded = false; // Call get_settings method for the acitivity/module. // Get instance of the mod's date exractor class. $mod = report_editdates_mod_date_extractor::make($cm->modname, $course); if ($mod && ($cmdatesettings = $mod->get_settings($cm))) { // Added activity name on the form. foreach ($cmdatesettings as $cmdatetype => $cmdatesetting) { $elname = 'date_mod_' . $cm->id . '_' . $cmdatetype; $mform->addElement($cmdatesetting->type, $elname, $cmdatesetting->label, array('optional' => $cmdatesetting->isoptional, 'step' => $cmdatesetting->getstep)); $mform->setDefault($elname, $cmdatesetting->currentvalue); if ($ismodreadonly) { $mform->hardFreeze($elname); } $elementadded++; $isdateadded = true; } } if ($coursehasavailability && $cm->availability) { // If there are retricted access date settings. if (strpos($cm->availability, '"type":"date"') !== false) { $editsettingurl = new moodle_url('/course/modedit.php', array('update' => $cm->id)); $editsettingurltext = html_writer::tag('a', get_string('editrestrictedaccess', 'report_editdates'), array('href' => $editsettingurl->out(false), 'target' => '_blank')); $mform->addElement('static', '', get_string('hasrestrictedaccess', 'report_editdates', $cm->name), $editsettingurltext); } } // Completion tracking. if ($coursehascompletion && $cm->completion) { $elname = 'date_mod_' . $cm->id . '_completionexpected'; $mform->addElement('date_selector', $elname, get_string('completionexpected', 'completion'), array('optional' => true)); $mform->addHelpButton($elname, 'completionexpected', 'completion'); $mform->setDefault($elname, $cm->completionexpected); if ($ismodreadonly) { $mform->hardFreeze($elname); } $elementadded++; $isdateadded = true; } if ($isdateadded) { $ismodadded = true; $addactionbuttons = true; } else { $mform->removeElement('modname' . $cm->id); } } // End of modules loop. if (!$ismodadded && $mform->elementExists($sectionname)) { $mform->removeElement($sectionname); } } } // End of sections loop. // Fetching all the blocks added directly under the course. // That is, parentcontextid = coursecontextid. $courseblocks = $DB->get_records('block_instances', array('parentcontextid' => $coursecontext->id)); // Check capability of current user. $canmanagesiteblocks = has_capability('moodle/site:manageblocks', $coursecontext); $anyblockadded = false; if ($courseblocks) { // Header for blocks. $mform->addElement('header', 'blockdatesection'); // Iterate though blocks array. foreach ($courseblocks as $blockid => $block) { $blockdatextrator = report_editdates_block_date_extractor::make($block->blockname, $course); if ($blockdatextrator) { // Create the block instance. $blockobj = block_instance($block->blockname, $block, $PAGE); // If get_settings returns a valid array. if ($blockdatesettings = $blockdatextrator->get_settings($blockobj)) { $anyblockadded = true; $addactionbuttons = true; // Adding block's Title on page. $mform->addElement('static', 'blocktitle', $blockobj->title); foreach ($blockdatesettings as $blockdatetype => $blockdatesetting) { $elname = 'date_block_' . $block->id . '_' . $blockdatetype; // Add element. $mform->addElement($blockdatesetting->type, $elname, $blockdatesetting->label, array('optional' => $blockdatesetting->isoptional, 'step' => $blockdatesetting->getstep)); $mform->setDefault($elname, $blockdatesetting->currentvalue); if (!$canmanagesiteblocks || !$blockobj->user_can_edit()) { $mform->hardFreeze($elname); } $elementadded++; } } } } } if (!$anyblockadded && $mform->elementExists('blockdatesection')) { $mform->removeElement('blockdatesection'); } // Adding submit/cancel buttons @ the end of the form. if ($addactionbuttons && $elementadded > 0) { $this->add_action_buttons(); } else { // Remove top action button. $mform->removeElement('buttonar'); } }
} // Update mod date settings. foreach ($moddatesettings as $modid => $datesettings) { $cm = $cms[$modid]; $mod = report_editdates_mod_date_extractor::make($cm->modname, $course); if ($mod) { $mod->save_dates($cm, $datesettings); } } // Update block date settings. $courseblocks = $DB->get_records("block_instances", array('parentcontextid' => $coursecontext->id)); foreach ($blockdatesettings as $blockid => $datesettings) { $block = $courseblocks[$blockid]; $blockobj = block_instance($block->blockname, $block, $PAGE); if ($blockobj->user_can_edit()) { $blockdatextrator = report_editdates_block_date_extractor::make($block->blockname, $course); if ($blockdatextrator) { $blockdatextrator->save_dates($blockobj, $datesettings); } } } // Commit transaction and finish up. $transaction->allow_commit(); rebuild_course_cache($course->id); redirect($PAGE->url); } } // Prepare activity type menu. $select = new single_select($baseurl, 'activitytype', $activitytypes, $activitytype, null, 'activitytypeform'); $select->set_label(get_string('activitytypefilter', 'report_editdates')); $select->set_help_icon('activitytypefilter', 'report_editdates');