/** * Create a new subpage instance * * @param array|stdClass $record The record to insert to subpage table * @param mod_subpage $subpage Subpage object * @param array $options Optional parameters * @return mod_subpage Subpage object created */ public function create_instance($record = null, array $options = null) { global $DB, $CFG; $record = (object) (array) $record; if (!isset($record->enablesharing)) { $record->enablesharing = 0; } $result = parent::create_instance($record, (array) $options); if (!empty($record->addsection)) { require_once $CFG->dirroot . '/mod/subpage/locallib.php'; $subobj = mod_subpage::get_from_cmid($result->cmid); $subobj->add_section(); } return $result; }
/** * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * * @global object * @param int $id * @return bool */ function subpage_delete_instance($id) { global $DB, $PAGE, $CFG; if (!($subpage = $DB->get_record("subpage", array("id" => $id)))) { return false; } require_once $CFG->dirroot . '/mod/subpage/locallib.php'; $transaction = $DB->start_delegated_transaction(); // if deleting a subpage activity from course/mod.php page (not delete the whole course) if ($PAGE->pagetype == 'course-mod') { $subpagecmid = required_param('delete', PARAM_INT); // Check if all the sections in this subpage is empty $subpageinstance = mod_subpage::get_from_cmid($subpagecmid); $subpagesections = $DB->get_records('subpage_sections', array("subpageid" => $subpage->id), '', 'sectionid'); foreach ($subpagesections as $sections) { if (!$subpageinstance->is_section_empty($sections->sectionid)) { // Section is not empty $url = new moodle_url('/mod/subpage/view.php', array('id' => $subpagecmid)); print_error('error_deletingsubpage', 'mod_subpage', $url); } } // all sections are empty, delete the empty sections foreach ($subpagesections as $sections) { $subpageinstance->delete_section($sections->sectionid); } } // Delete main table and sections $DB->delete_records("subpage", array("id" => $subpage->id)); $DB->delete_records("subpage_sections", array("subpageid" => $subpage->id)); // If there are any shared subpages that reference this, rebuild those // courses so that they reflect the deletion if ($DB->get_field('modules', 'id', array('name' => 'sharedsubpage'))) { $references = $DB->get_records('sharedsubpage', array('subpageid' => $subpage->id), 'id,course'); foreach ($references as $sharedsubpage) { rebuild_course_cache($sharedsubpage->course, true); } } $transaction->allow_commit(); return true; }
$addsection = optional_param('addsection', 0, PARAM_INT); $recache = optional_param('recache', 0, PARAM_INT); $cancelcopy = optional_param('cancelcopy', 0, PARAM_BOOL); if (empty($cmid)) { print_error('unspecifysubpageid', 'subpage'); } if (!empty($cancelcopy) && confirm_sesskey()) { unset($USER->activitycopy); unset($USER->activitycopycourse); unset($USER->activitycopyname); redirect("view.php?id={$cmid}"); } // This must be done first because some horrible combination of junk means that // page might be initialised before we expect. $PAGE->set_pagelayout('incourse'); $subpage = mod_subpage::get_from_cmid($cmid); $course = $subpage->get_course(); // Defined here to avoid notices on errors etc. $thisurl = new moodle_url('/mod/subpage/view.php', array('id' => $cmid)); $PAGE->set_url($thisurl); $PAGE->set_cm($subpage->get_course_module()); $modcontext = context_module::instance($cmid); require_login($course, true, $subpage->get_course_module()); require_capability('mod/subpage:view', $modcontext); $event = \mod_subpage\event\course_module_viewed::create(array('objectid' => $subpage->get_course_module()->instance, 'context' => $modcontext)); $event->add_record_snapshot('course', $course); $event->trigger(); if (!empty($recache) && confirm_sesskey()) { $context = context_course::instance($subpage->get_course()->id); require_capability('moodle/course:manageactivities', $context); rebuild_course_cache($course->id, true);
/** * Obtains last subpage section id for a given subpage. * @param integer $subpagecmid * @return last section id for a given subpage */ public function get_last_subpage_section_id($subpagecmid) { global $DB, $CFG; $sql = "SELECT sectionid\n FROM {subpage_sections}\n WHERE subpageid = (select instance from {course_modules} where id = ?)\n ORDER BY pageorder DESC"; $params = array($subpagecmid); $records = $DB->get_records_sql($sql, $params, 0, 1); if (count($records) == 0) { // When there are no sections, add one require_once $CFG->dirroot . '/mod/subpage/locallib.php'; $subpage = mod_subpage::get_from_cmid($subpagecmid); $subpage->add_section(); // Redo the query $records = $DB->get_records_sql($sql, $params, 0, 1); if (count($records) == 0) { throw new coding_exception("No section defined in subpage {$subpagecmid}"); } } return reset($records)->sectionid; }
if (substr($key, 0, 3) === 'mod' && $data == 1) { $cmids[] = substr($key, 3); } } if (empty($cmids)) { echo $OUTPUT->header(); // Course wrapper start. echo html_writer::start_tag('div', array('class' => 'course-content')); echo $OUTPUT->notification(get_string('nomodulesselected', 'mod_subpage')); echo $OUTPUT->continue_button("{$CFG->wwwroot}/mod/subpage/view.php?id={$cmid}"); echo html_writer::end_tag('div'); echo $OUTPUT->footer(); exit; } else { // destination is either null or a subpage $dest = $info[0] !== 'course' ? mod_subpage::get_from_cmid($info[0]) : null; if ($createnew && $dest) { $newsection = $dest->add_section(); $id = $newsection['sectionid']; } else { $id = $info[1]; } // ensure that the destination section does exists if (!($section = $DB->get_record('course_sections', array('id' => (int) $id)))) { print_error('sectionnotcreatedorexisting', 'mod_subpage', "{$CFG->wwwroot}/mod/subpage/view.php?id={$cmid}"); } foreach ($cmids as $id) { if (!($cm = get_coursemodule_from_id('', $id))) { print_error('modulenotfound', 'mod_subpage', "{$CFG->wwwroot}/mod/subpage/view.php?id={$cmid}"); } // no reason to move if in the same section
/** * Returns arrays of all cm ids and all section ids * used in the subpage * @param mod_subpage $subpage * @return array activities,sections */ public function get_includes($subpage) { $cm = $subpage->get_course_module(); $cmids = array($cm->id); $sids = array(); $modinfo = get_fast_modinfo($subpage->get_course(), -1); $sections = $modinfo->get_sections(); $spsections = $subpage->get_sections(); foreach ($spsections as $sid => $info) { $sids[] = $sid; if (!empty($sections[$info->section])) { foreach ($sections[$info->section] as $cmid) { $cmids[] = $cmid; $cminfo = $modinfo->get_cm($cmid); if (strtolower($cminfo->get_module_type_name()) == 'subpage') { // Support subpage in subpage. $asubpage = \mod_subpage::get_from_cmid($cmid); list($acmids, $asids) = $this->get_includes($asubpage); $cmids = array_merge($cmids, $acmids); $sids = array_merge($sids, $asids); } } } } return array($cmids, $sids); }
public function test_subpage_mod_subpage_class() { // setup a course and module $course = $this->get_new_course(); $coursesection = $this->get_new_course_section($course->id); $subpage = $this->get_new_subpage($course->id); $cm = $this->get_new_course_module($course->id, $subpage->id, $coursesection->id); $testname = $subpage->name; $testintro = $subpage->intro; // get the subpage object $module = mod_subpage::get_from_cmid($cm->id); $this->assertIsA($module, "mod_subpage"); $this->assertTrue($module->has_intro()); $this->assertEqual($module->get_name(), $testname); $this->assertEqual($module->get_intro(), $testintro); $subpage = $module->get_subpage(); $this->assertIsA($subpage, "stdClass"); $this->assertEqual($subpage->name, $testname); // testing adding sections $sid1 = 2; $sectionname1 = "sectionname1"; $sectionsummary1 = "sectionsummary1"; $section1 = $module->add_section($sectionname1, $sectionsummary1); $this->assertIsA($section1, "array"); $sid2 = 3; $sectionname2 = "sectionname2"; $sectionsummary2 = "sectionsummary2"; $section2 = $module->add_section($sectionname2, $sectionsummary2); $this->assertIsA($section2, "array"); $sid3 = 4; $sectionname3 = "sectionname3"; $sectionsummary3 = "sectionsummary3"; $section3 = $module->add_section($sectionname3, $sectionsummary3); $this->assertIsA($section3, "array"); // test getting sections $sections = $module->get_sections(); $this->assertEqual($sections[$section1['sectionid']]->name, $sectionname1); $this->assertEqual($sections[$section1['sectionid']]->summary, "<div class=\"text_to_html\">{$sectionsummary1}</div>"); $this->assertEqual($sections[$section2['sectionid']]->name, $sectionname2); $this->assertEqual($sections[$section2['sectionid']]->summary, "<div class=\"text_to_html\">{$sectionsummary2}</div>"); $this->assertEqual($sections[$section3['sectionid']]->name, $sectionname3); $this->assertEqual($sections[$section3['sectionid']]->summary, "<div class=\"text_to_html\">{$sectionsummary3}</div>"); // test last section order $this->assertEqual($module->get_last_section_pageorder(), 3); // test moving a section - move first section to the end $module->move_section($section1['sectionid'], 3); // check if all page orders have been updated correctly $sections = $module->get_sections(); $this->assertEqual($sections[$section1['sectionid']]->pageorder, 3); $this->assertEqual($sections[$section2['sectionid']]->pageorder, 1); $this->assertEqual($sections[$section3['sectionid']]->pageorder, 2); // test deleting a section $module->delete_section($section3['sectionid']); $this->assertEqual($module->get_last_section_pageorder(), 2); $sections = $module->get_sections(); $this->assertEqual($sections[$section1['sectionid']]->pageorder, 2); $this->assertEqual($sections[$section2['sectionid']]->pageorder, 1); // test section stealthing $this->assertFalse($sections[$section2['sectionid']]->stealth); $stealth = 1; $module->set_section_stealth($sid2, $stealth); $sections = $module->get_sections(); //refresh from db $this->assertTrue($sections[$section2['sectionid']]->stealth); }