/**
  * setUp
  */
 public function setUp()
 {
     global $CFG;
     require_once $CFG->libdir . '/completionlib.php';
     // turn on completion and availability
     $CFG->enablecompletion = true;
     $CFG->enableavailability = true;
     // create a course
     $this->_course = $this->getDataGenerator()->create_course(['numsections' => self::NUM_SECTIONS, 'enablecompletion' => COMPLETION_ENABLED], ['createsections' => true]);
     // section with two unavailable activities (an invisible forum and a visible quiz)
     $this->getDataGenerator()->create_module('forum', ['course' => $this->_course->id, 'section' => 3, 'completion' => COMPLETION_TRACKING_NONE, 'availability' => json_encode(['op' => core_availability\tree::OP_AND, 'c' => [availability_date\condition::get_json('>=', time() + 24 * 3600)], 'showc' => [false]])]);
     $this->_quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->_course->id, 'section' => 3, 'completion' => COMPLETION_TRACKING_NONE, 'availability' => json_encode(['op' => core_availability\tree::OP_AND, 'c' => [availability_date\condition::get_json('>=', time() + 24 * 3600)], 'showc' => [true]])]);
     // a hidden section
     set_section_visible($this->_course->id, 4, 0);
     $this->getDataGenerator()->create_module('forum', ['course' => $this->_course->id, 'section' => 4, 'completion' => COMPLETION_TRACKING_NONE]);
     // section with one activity
     $this->_forum = $this->getDataGenerator()->create_module('forum', ['course' => $this->_course->id, 'section' => 5, 'completion' => COMPLETION_TRACKING_NONE]);
     // section with two activities
     $this->_wiki = $this->getDataGenerator()->create_module('wiki', ['course' => $this->_course->id, 'section' => 6, 'completion' => COMPLETION_TRACKING_MANUAL]);
     $this->getDataGenerator()->create_module('quiz', ['course' => $this->_course->id, 'section' => 6, 'completion' => COMPLETION_TRACKING_NONE, 'visible' => 0]);
     $course_modinfo = get_fast_modinfo($this->_course);
     $this->_section_info_all = $course_modinfo->get_section_info_all();
     $this->resetAfterTest();
 }
Beispiel #2
0
     }
 }
 if ($modchooser == 1 && confirm_sesskey()) {
     set_user_preference('usemodchooser', $modchooser);
 } else {
     if ($modchooser == 0 && confirm_sesskey()) {
         set_user_preference('usemodchooser', $modchooser);
     }
 }
 if (has_capability('moodle/course:update', $context)) {
     if ($hide && confirm_sesskey()) {
         set_section_visible($course->id, $hide, '0');
         redirect($PAGE->url);
     }
     if ($show && confirm_sesskey()) {
         set_section_visible($course->id, $show, '1');
         redirect($PAGE->url);
     }
     if (!empty($section)) {
         if (!empty($move) and confirm_sesskey()) {
             if (move_section($course, $section, $move)) {
                 if ($course->id == SITEID) {
                     redirect($CFG->wwwroot . '/?redirect=0');
                 } else {
                     redirect(course_get_url($course));
                 }
             } else {
                 echo $OUTPUT->notification('An error occurred while moving a section');
             }
         }
     }
Beispiel #3
0
// MDL-10221 the DELETE method is not allowed on some web servers, so we simulate it with the action URL param
$requestmethod = $_SERVER['REQUEST_METHOD'];
if ($pageaction == 'DELETE') {
    $requestmethod = 'DELETE';
}
switch ($requestmethod) {
    case 'POST':
        switch ($class) {
            case 'section':
                if (!$DB->record_exists('course_sections', array('course' => $course->id, 'section' => $id))) {
                    throw new moodle_exception('AJAX commands.php: Bad Section ID ' . $id);
                }
                switch ($field) {
                    case 'visible':
                        require_capability('moodle/course:sectionvisibility', $coursecontext);
                        $resourcestotoggle = set_section_visible($course->id, $id, $value);
                        echo json_encode(array('resourcestotoggle' => $resourcestotoggle));
                        break;
                    case 'move':
                        require_capability('moodle/course:movesections', $coursecontext);
                        move_section_to($course, $id, $value);
                        // See if format wants to do something about it
                        $response = course_get_format($course)->ajax_section_move();
                        if ($response !== null) {
                            echo json_encode($response);
                        }
                        break;
                }
                break;
            case 'resource':
                switch ($field) {
Beispiel #4
0
 /**
  * Sets the section visible/hidden including subsections and modules
  *
  * @param int|stdClass|section_info $section
  * @param int $visibility
  * @param null|int $setvisibleold if specified in case of hiding the section,
  *    this will be the value of visibleold for the section $section.
  */
 protected function set_section_visible($section, $visibility, $setvisibleold = null)
 {
     $subsections = array();
     $sectionnumber = $this->get_section_number($section);
     if (!$sectionnumber && !$visibility) {
         // can not hide section with number 0
         return;
     }
     $section = $this->get_section($section);
     if ($visibility && $section->parent && !$this->get_section($section->parent)->visible) {
         // can not set section visible when parent is hidden
         return;
     }
     $ch = array($section);
     while (!empty($ch)) {
         $chlast = $ch;
         $ch = array();
         foreach ($chlast as $s) {
             // store copy of attributes to avoid rebuilding course cache when we need to access section properties
             $subsections[] = (object) array('section' => $s->section, 'id' => $s->id, 'visible' => $s->visible, 'visibleold' => $s->visibleold);
             $ch += $this->get_subsections($s);
         }
     }
     foreach ($subsections as $s) {
         if ($s->section == $sectionnumber) {
             set_section_visible($this->courseid, $s->section, $visibility);
             if ($setvisibleold === null) {
                 $setvisibleold = $visibility;
             }
             $this->update_section_format_options(array('id' => $s->id, 'visibleold' => $setvisibleold));
         } else {
             if ($visibility) {
                 if ($s->visibleold) {
                     set_section_visible($this->courseid, $s->section, $s->visibleold);
                 }
             } else {
                 if ($s->visible) {
                     set_section_visible($this->courseid, $s->section, $visibility);
                     $this->update_section_format_options(array('id' => $s->id, 'visibleold' => $s->visible));
                 }
             }
         }
     }
 }
Beispiel #5
0
 /**
  * Make sure that current active activity is in section 0
  *
  * All other activities are moved to section 1 that will be displayed as 'Orphaned'.
  * It may be needed after the course format was changed or activitytype in
  * course settings has been changed.
  *
  * @return null|cm_info current activity
  */
 public function reorder_activities()
 {
     course_create_sections_if_missing($this->courseid, array(0, 1));
     foreach ($this->get_sections() as $sectionnum => $section) {
         if ($sectionnum && $section->visible || !$sectionnum && !$section->visible) {
             // Make sure that 0 section is visible and all others are hidden.
             set_section_visible($this->courseid, $sectionnum, $sectionnum == 0);
         }
     }
     $modinfo = get_fast_modinfo($this->courseid);
     // Find the current activity (first activity with the specified type in all course activities).
     $activitytype = $this->get_activitytype();
     $activity = null;
     if (!empty($activitytype)) {
         foreach ($modinfo->sections as $sectionnum => $cmlist) {
             foreach ($cmlist as $cmid) {
                 if ($modinfo->cms[$cmid]->modname === $activitytype) {
                     $activity = $modinfo->cms[$cmid];
                     break 2;
                 }
             }
         }
     }
     // Make sure the current activity is in the 0-section.
     if ($activity && $activity->sectionnum != 0) {
         moveto_module($activity, $modinfo->get_section_info(0));
         // Cache was reset so get modinfo again.
         $modinfo = get_fast_modinfo($this->courseid);
     }
     // Move all other activities into section 1 (the order must be kept).
     $hasvisibleactivities = false;
     $firstorphanedcm = null;
     foreach ($modinfo->sections as $sectionnum => $cmlist) {
         if ($sectionnum && !empty($cmlist) && $firstorphanedcm === null) {
             $firstorphanedcm = reset($cmlist);
         }
         foreach ($cmlist as $cmid) {
             if ($sectionnum > 1) {
                 moveto_module($modinfo->get_cm($cmid), $modinfo->get_section_info(1));
             } else {
                 if (!$hasvisibleactivities && $sectionnum == 1 && $modinfo->get_cm($cmid)->visible) {
                     $hasvisibleactivities = true;
                 }
             }
         }
     }
     if (!empty($modinfo->sections[0])) {
         foreach ($modinfo->sections[0] as $cmid) {
             if (!$activity || $cmid != $activity->id) {
                 moveto_module($modinfo->get_cm($cmid), $modinfo->get_section_info(1), $firstorphanedcm);
             }
         }
     }
     if ($hasvisibleactivities) {
         set_section_visible($this->courseid, 1, false);
     }
     return $activity;
 }
Beispiel #6
0
 /**
  * Tests moving a module between hidden/visible sections and
  * verifies that the course/module visiblity seettings are
  * retained.
  */
 public function test_moveto_module_between_hidden_sections()
 {
     global $DB;
     $this->resetAfterTest(true);
     $course = $this->getDataGenerator()->create_course(array('numsections' => 4), array('createsections' => true));
     $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
     $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
     $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
     // Set the page as hidden
     set_coursemodule_visible($page->cmid, 0);
     // Set sections 3 as hidden.
     set_section_visible($course->id, 3, 0);
     $modinfo = get_fast_modinfo($course);
     $hiddensection = $modinfo->get_section_info(3);
     // New section is definitely not visible:
     $this->assertEquals($hiddensection->visible, 0);
     $forumcm = $modinfo->cms[$forum->cmid];
     $pagecm = $modinfo->cms[$page->cmid];
     // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
     $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
     $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
     $modinfo = get_fast_modinfo($course);
     // Verify that forum and page have been moved to the hidden section and quiz has not.
     $this->assertContains($forum->cmid, $modinfo->sections[3]);
     $this->assertContains($page->cmid, $modinfo->sections[3]);
     $this->assertNotContains($quiz->cmid, $modinfo->sections[3]);
     // Verify that forum has been made invisible.
     $forumcm = $modinfo->cms[$forum->cmid];
     $this->assertEquals($forumcm->visible, 0);
     // Verify that old state has been retained.
     $this->assertEquals($forumcm->visibleold, 1);
     // Verify that page has stayed invisible.
     $pagecm = $modinfo->cms[$page->cmid];
     $this->assertEquals($pagecm->visible, 0);
     // Verify that old state has been retained.
     $this->assertEquals($pagecm->visibleold, 0);
     // Verify that quiz has been unaffected.
     $quizcm = $modinfo->cms[$quiz->cmid];
     $this->assertEquals($quizcm->visible, 1);
     // Move forum and page back to visible section.
     // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
     $visiblesection = $modinfo->get_section_info(2);
     $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
     $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
     $modinfo = get_fast_modinfo($course);
     // Double check that forum has been made visible.
     $forumcm = $modinfo->cms[$forum->cmid];
     $this->assertEquals($forumcm->visible, 1);
     // Double check that page has stayed invisible.
     $pagecm = $modinfo->cms[$page->cmid];
     $this->assertEquals($pagecm->visible, 0);
     // Move the page in the same section (this is what mod duplicate does).
     // Visibility of page remains 0.
     $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
     // Double check that the the page is still hidden.
     $modinfo = get_fast_modinfo($course);
     $pagecm = $modinfo->cms[$page->cmid];
     $this->assertEquals($pagecm->visible, 0);
 }
Beispiel #7
0
             // Misleading case. Should probably call it 'move'.
             // We want to move the block around. This means changing
             // the column (position field) and/or block sort order
             // (weight field).
             blocks_move_block($PAGE, $blockinstance, $column, $value);
             break;
     }
     break;
 case 'section':
     if (!record_exists('course_sections', 'course', $course->id, 'section', $id)) {
         error_log('AJAX commands.php: Bad Section ID ' . $id);
         die;
     }
     switch ($field) {
         case 'visible':
             set_section_visible($course->id, $id, $value);
             break;
         case 'move':
             move_section($course, $id, $value);
             break;
     }
     rebuild_course_cache($course->id);
     break;
 case 'resource':
     if (!($mod = get_record('course_modules', 'id', $id, 'course', $course->id))) {
         error_log('AJAX commands.php: Bad course module ID ' . $id);
         die;
     }
     switch ($field) {
         case 'visible':
             set_coursemodule_visible($mod->id, $value);
Beispiel #8
0
 function set_section_visible($course_id, $section, $active)
 {
     global $CFG, $DB;
     set_section_visible($course_id, $section, $active);
 }
 public static function observer(\core\event\base $event)
 {
     global $DB;
     $unlocked_content = false;
     if (!block_game_content_unlock_helper::is_student($event->userid)) {
         return;
     }
     $uss = $DB->get_records_sql("SELECT * FROM {content_unlock_system} WHERE deleted = ? AND " . $DB->sql_compare_text('conditions') . " = " . $DB->sql_compare_text('?'), array('deleted' => 0, 'conditions' => $event->eventname));
     foreach ($uss as $unlocksystem) {
         $ccm = get_course_and_cm_from_cmid($unlocksystem->coursemoduleid);
         if ($event->courseid != $ccm[0]->id) {
             continue;
         }
         if (!(content_unlock_satisfies_conditions($unlocksystem->restrictions, $event->courseid, $event->userid) && content_unlock_satisfies_advanced_conditions($unlocksystem, $event) && content_unlock_satisfies_block_conditions($unlocksystem, $event->courseid, $event->userid))) {
             continue;
         }
         $blockcontextid = $DB->get_field('block_instances', 'parentcontextid', array('id' => $unlocksystem->blockinstanceid));
         if (!$blockcontextid) {
             continue;
         }
         $blockcontext = context::instance_by_id($blockcontextid);
         $context = context::instance_by_id($event->contextid);
         if (strpos($context->path, $blockcontext->path) !== 0 && $blockcontext->instanceid != SITEID) {
             continue;
         }
         $sql = "SELECT count(c.id)\n\t\t\t\tFROM {content_unlock_log} c\n\t\t\t\t\tINNER JOIN {logstore_standard_log} l ON c.logid = l.id\n\t\t\t\tWHERE l.userid = :userid\n\t\t\t\t\tAND c.unlocksystemid = :unlocksystemid";
         $params['userid'] = $event->userid;
         $params['unlocksystemid'] = $unlocksystem->id;
         if ($DB->count_records_sql($sql, $params) > 0) {
             continue;
         }
         $manager = get_log_manager();
         $selectreaders = $manager->get_readers('\\core\\log\\sql_reader');
         if ($selectreaders) {
             $reader = reset($selectreaders);
         }
         $selectwhere = "eventname = :eventname\n\t\t\t\tAND component = :component\n\t\t\t\tAND action = :action\n\t\t\t\tAND target = :target\n\t\t\t\tAND crud = :crud\n\t\t\t\tAND edulevel = :edulevel\n\t\t\t\tAND contextid = :contextid\n\t\t\t\tAND contextlevel = :contextlevel\n\t\t\t\tAND contextinstanceid = :contextinstanceid\n\t\t\t\tAND userid = :userid \n\t\t\t\tAND anonymous = :anonymous\n\t\t\t\tAND timecreated = :timecreated";
         $params['eventname'] = $event->eventname;
         $params['component'] = $event->component;
         $params['action'] = $event->action;
         $params['target'] = $event->target;
         $params['crud'] = $event->crud;
         $params['edulevel'] = $event->edulevel;
         $params['contextid'] = $event->contextid;
         $params['contextlevel'] = $event->contextlevel;
         $params['contextinstanceid'] = $event->contextinstanceid;
         $params['userid'] = $event->userid;
         $params['anonymous'] = $event->anonymous;
         $params['timecreated'] = $event->timecreated;
         $logid = $reader->get_events_select($selectwhere, $params, '', 0, 0);
         $logid = array_keys($logid)[0];
         $record = new stdClass();
         $record->logid = $logid;
         $record->unlocksystemid = $unlocksystem->id;
         $DB->insert_record('content_unlock_log', $record);
         $unlocked_content = true;
         if ($unlocksystem->mode == 0) {
             if ($unlocksystem->coursemodulevisibility == 1) {
                 set_section_visible($event->courseid, $ccm[1]->sectionnum, 1);
             }
             set_coursemodule_visible($unlocksystem->coursemoduleid, $unlocksystem->coursemodulevisibility);
         } else {
             groups_add_member($unlocksystem->groupid, $event->userid);
         }
     }
     if ($unlocked_content) {
         $params = array('context' => $context);
         $event = \block_game_content_unlock\event\content_unlocked::create($params);
         $event->trigger();
     }
 }
Beispiel #10
0
 public function test_section_visibility()
 {
     $this->setAdminUser();
     $this->resetAfterTest(true);
     // Create course.
     $course = $this->getDataGenerator()->create_course(array('numsections' => 3), array('createsections' => true));
     // Testing an empty section.
     $sectionnumber = 1;
     set_section_visible($course->id, $sectionnumber, 0);
     $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
     $this->assertEquals($section_info->visible, 0);
     set_section_visible($course->id, $sectionnumber, 1);
     $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
     $this->assertEquals($section_info->visible, 1);
     // Testing a section with visible modules.
     $sectionnumber = 2;
     $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), array('section' => $sectionnumber));
     $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id), array('section' => $sectionnumber));
     $modules = compact('forum', 'assign');
     set_section_visible($course->id, $sectionnumber, 0);
     $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
     $this->assertEquals($section_info->visible, 0);
     foreach ($modules as $mod) {
         $this->check_module_visibility($mod, 0, 1);
     }
     set_section_visible($course->id, $sectionnumber, 1);
     $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
     $this->assertEquals($section_info->visible, 1);
     foreach ($modules as $mod) {
         $this->check_module_visibility($mod, 1, 1);
     }
     // Testing a section with hidden modules, which should stay hidden.
     $sectionnumber = 3;
     $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), array('section' => $sectionnumber));
     $assign = $this->getDataGenerator()->create_module('assign', array('duedate' => time(), 'course' => $course->id), array('section' => $sectionnumber));
     $modules = compact('forum', 'assign');
     foreach ($modules as $mod) {
         set_coursemodule_visible($mod->cmid, 0);
         $this->check_module_visibility($mod, 0, 0);
     }
     set_section_visible($course->id, $sectionnumber, 0);
     $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
     $this->assertEquals($section_info->visible, 0);
     foreach ($modules as $mod) {
         $this->check_module_visibility($mod, 0, 0);
     }
     set_section_visible($course->id, $sectionnumber, 1);
     $section_info = get_fast_modinfo($course->id)->get_section_info($sectionnumber);
     $this->assertEquals($section_info->visible, 1);
     foreach ($modules as $mod) {
         $this->check_module_visibility($mod, 0, 0);
     }
 }
 /**
  * Handle any pre-display arguments for the course. Make legacy variables global.
  * TODO - Move arguments into object structure.
  *
  */
 function handle_args()
 {
     global $CFG, $USER, $PAGE, $SESSION, $course;
     global $section, $marker;
     /// These are needed in various library functions.
     $section = optional_param('section', 0, PARAM_INT);
     $marker = optional_param('marker', -1, PARAM_INT);
     $edit = optional_param('edit', -1, PARAM_BOOL);
     $hide = optional_param('hide', 0, PARAM_INT);
     $show = optional_param('show', 0, PARAM_INT);
     $move = optional_param('move', 0, PARAM_INT);
     if (!isset($USER->editing)) {
         $USER->editing = 0;
     }
     if ($PAGE->user_allowed_editing()) {
         if ($edit == 1 and confirm_sesskey()) {
             $USER->editing = 1;
         } else {
             if ($edit == 0 and confirm_sesskey()) {
                 $USER->editing = 0;
                 if (!empty($USER->activitycopy) && $USER->activitycopycourse == $course->id) {
                     $USER->activitycopy = false;
                     $USER->activitycopycourse = NULL;
                 }
             }
         }
         if ($hide && confirm_sesskey()) {
             set_section_visible($course->id, $hide, '0');
         }
         if ($show && confirm_sesskey()) {
             set_section_visible($course->id, $show, '1');
         }
         if (!empty($section)) {
             if (!empty($move) and confirm_sesskey()) {
                 if (!move_section($course, $section, $move)) {
                     notify('An error occurred while moving a section');
                 }
             }
         }
     } else {
         $USER->editing = 0;
     }
     $SESSION->fromdiscussion = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
     if ($course->id == SITEID) {
         // This course is not a real course.
         redirect($CFG->wwwroot . '/');
     }
 }
if(!confirm_sesskey()){
	$responseobj->success = 0;
	$responseobj->reason = "Invalid session";
	exit;
}

switch($action){
	case ACTION_SHOW :
		$responseobj->success = 1;
		$responseobj->reason = "show";
        set_section_visible($course_id, $section_id, '1');
        break;
	case ACTION_HIDE :
		$responseobj->success = 1;
		$responseobj->reason = "hide";
        set_section_visible($course_id, $section_id, '0');
        break;
	case ACTION_MARK :
		$context = get_context_instance(CONTEXT_COURSE, $course_id);
        if (has_capability('moodle/course:setcurrentsection', $context)) {
            //$course->marker = $marker;
            $DB->set_field("course", "marker", $section_id, array("id"=>$course_id));
        }
        $responseobj->success = 1;
		$responseobj->reason = $section_id ? "marked" : "cleared";
		break;
	case ACTION_MOVE :
		if(empty($move)){
			$responseobj->success = 0;
		    $responseobj->reason = "No movement specified";
		} else {
Beispiel #13
0
 public function set_section_visibility($shortname, $sectionnumber, $visible)
 {
     global $OUTPUT;
     $course = $this->coursebyshortname($shortname);
     $context = \context_course::instance($course->id);
     require_capability('moodle/course:sectionvisibility', $context);
     // Note, we do not use the return value of set_section_visible (resourcestotoggle) as nested resource visibility
     // is handled via CSS.
     set_section_visible($course->id, $sectionnumber, $visible);
     $modinfo = get_fast_modinfo($course);
     $section = $modinfo->get_section_info($sectionnumber);
     $actionmodel = new \theme_snap\renderables\course_action_section_visibility($course, $section);
     $toc = new \theme_snap\renderables\course_toc($course);
     return ['actionmodel' => $actionmodel->export_for_template($OUTPUT), 'toc' => $toc->export_for_template($OUTPUT)];
 }