예제 #1
0
 /**
  * Tests changing the visibility of a course.
  */
 public function test_course_change_visibility()
 {
     global $DB;
     $this->resetAfterTest(true);
     $generator = $this->getDataGenerator();
     $category = $generator->create_category();
     $course = $generator->create_course(array('category' => $category->id));
     $this->assertEquals('1', $course->visible);
     $this->assertEquals('1', $course->visibleold);
     $this->assertTrue(course_change_visibility($course->id, false));
     $course = $DB->get_record('course', array('id' => $course->id));
     $this->assertEquals('0', $course->visible);
     $this->assertEquals('0', $course->visibleold);
     $this->assertTrue(course_change_visibility($course->id, true));
     $course = $DB->get_record('course', array('id' => $course->id));
     $this->assertEquals('1', $course->visible);
     $this->assertEquals('1', $course->visibleold);
 }
예제 #2
0
 /**
  * Makes a course hidden given a \course_in_list object.
  *
  * @param \course_in_list $course
  * @return bool
  * @throws \moodle_exception
  */
 public static function action_course_hide(\course_in_list $course)
 {
     if (!$course->can_change_visibility()) {
         throw new \moodle_exception('permissiondenied', 'error', '', null, 'course_in_list::can_change_visbility');
     }
     return course_change_visibility($course->id, false);
 }
 /**
  * Test to confirm that subscriptions for users who fail can_access_course(), are deactivated.
  */
 public function test_can_access_course()
 {
     // Enrol the user as a teacher. This role should have the required capability.
     $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
     // Strip the ability to see hidden courses, so we'll fail the check_subscriptions->user_can_access_course call.
     $context = \context_course::instance($this->course->id);
     assign_capability('moodle/course:viewhiddencourses', CAP_PROHIBIT, $this->teacherrole->id, $context);
     // Subscription should be active to start with.
     $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
     // Hide the course.
     course_change_visibility($this->course->id, false);
     // Run the task.
     $task = new \tool_monitor\task\check_subscriptions();
     $task->execute();
     // The subscription should be inactive. Pass in the id only to refetch the data.
     $this->reload_subscription();
     $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 }
예제 #4
0
파일: lib.php 프로젝트: evltuma/moodle
 /**
  * Process the group tag. This defines a Moodle course.
  *
  * @param string $tagcontents The raw contents of the XML element
  */
 protected function process_group_tag($tagcontents)
 {
     global $DB, $CFG;
     // Get configs.
     $truncatecoursecodes = $this->get_config('truncatecoursecodes');
     $createnewcourses = $this->get_config('createnewcourses');
     $updatecourses = $this->get_config('updatecourses');
     if ($createnewcourses) {
         require_once "{$CFG->dirroot}/course/lib.php";
     }
     // Process tag contents.
     $group = new stdClass();
     if (preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)) {
         $group->coursecode = trim($matches[1]);
     }
     $matches = array();
     if (preg_match('{<description>.*?<long>(.*?)</long>.*?</description>}is', $tagcontents, $matches)) {
         $group->long = trim($matches[1]);
     }
     $matches = array();
     if (preg_match('{<description>.*?<short>(.*?)</short>.*?</description>}is', $tagcontents, $matches)) {
         $group->short = trim($matches[1]);
     }
     $matches = array();
     if (preg_match('{<description>.*?<full>(.*?)</full>.*?</description>}is', $tagcontents, $matches)) {
         $group->full = trim($matches[1]);
     }
     if (preg_match('{<org>(.*?)</org>}is', $tagcontents, $matchesorg)) {
         if (preg_match_all('{<orgunit>(.*?)</orgunit>}is', $matchesorg[1], $matchesorgunit)) {
             $group->categories = array_map('trim', $matchesorgunit[1]);
         }
     }
     $recstatus = $this->get_recstatus($tagcontents, 'group');
     if (empty($group->coursecode)) {
         $this->log_line('Error: Unable to find course code in \'group\' element.');
     } else {
         // First, truncate the course code if desired.
         if (intval($truncatecoursecodes) > 0) {
             $group->coursecode = $truncatecoursecodes > 0 ? substr($group->coursecode, 0, intval($truncatecoursecodes)) : $group->coursecode;
         }
         // For compatibility with the (currently inactive) course aliasing, we need this to be an array.
         $group->coursecode = array($group->coursecode);
         // Third, check if the course(s) exist.
         foreach ($group->coursecode as $coursecode) {
             $coursecode = trim($coursecode);
             $dbcourse = $DB->get_record('course', array('idnumber' => $coursecode));
             if (!$dbcourse) {
                 if (!$createnewcourses) {
                     $this->log_line("Course {$coursecode} not found in Moodle's course idnumbers.");
                 } else {
                     // Create the (hidden) course(s) if not found.
                     $courseconfig = get_config('moodlecourse');
                     // Load Moodle Course shell defaults.
                     // New course.
                     $course = new stdClass();
                     foreach ($this->coursemappings as $courseattr => $imsname) {
                         if ($imsname == 'ignore') {
                             continue;
                         }
                         // Check if the IMS file contains the mapped tag, otherwise fallback on coursecode.
                         if ($imsname == 'coursecode') {
                             $course->{$courseattr} = $coursecode;
                         } else {
                             if (!empty($group->{$imsname})) {
                                 $course->{$courseattr} = $group->{$imsname};
                             } else {
                                 $this->log_line('No ' . $imsname . ' description tag found for ' . $coursecode . ' coursecode, using ' . $coursecode . ' instead');
                                 $course->{$courseattr} = $coursecode;
                             }
                         }
                     }
                     $course->idnumber = $coursecode;
                     $course->format = $courseconfig->format;
                     $course->visible = $courseconfig->visible;
                     $course->newsitems = $courseconfig->newsitems;
                     $course->showgrades = $courseconfig->showgrades;
                     $course->showreports = $courseconfig->showreports;
                     $course->maxbytes = $courseconfig->maxbytes;
                     $course->groupmode = $courseconfig->groupmode;
                     $course->groupmodeforce = $courseconfig->groupmodeforce;
                     $course->enablecompletion = $courseconfig->enablecompletion;
                     // Insert default names for teachers/students, from the current language.
                     // Handle course categorisation (taken from the group.org.orgunit or group.org.id fields if present).
                     $course->category = $this->get_category_from_group($group->categories);
                     $course->startdate = time();
                     // Choose a sort order that puts us at the start of the list!
                     $course->sortorder = 0;
                     $course = create_course($course);
                     $this->log_line("Created course {$coursecode} in Moodle (Moodle ID is {$course->id})");
                 }
             } else {
                 if ($recstatus == self::IMSENTERPRISE_UPDATE && $dbcourse) {
                     if ($updatecourses) {
                         // Update course. Allowed fields to be updated are:
                         // Short Name, and Full Name.
                         $hasupdates = false;
                         if (!empty($group->short)) {
                             if ($group->short != $dbcourse->shortname) {
                                 $dbcourse->shortname = $group->short;
                                 $hasupdates = true;
                             }
                         }
                         if (!empty($group->full)) {
                             if ($group->full != $dbcourse->fullname) {
                                 $dbcourse->fullname = $group->full;
                                 $hasupdates = true;
                             }
                         }
                         if ($hasupdates) {
                             update_course($dbcourse);
                             $courseid = $dbcourse->id;
                             $this->log_line("Updated course {$coursecode} in Moodle (Moodle ID is {$courseid})");
                         }
                     } else {
                         // Update courses option is not enabled. Ignore.
                         $this->log_line("Ignoring update to course {$coursecode}");
                     }
                 } else {
                     if ($recstatus == self::IMSENTERPRISE_DELETE && $dbcourse) {
                         // If course does exist, but recstatus==3 (delete), then set the course as hidden.
                         $courseid = $dbcourse->id;
                         $show = false;
                         course_change_visibility($courseid, $show);
                         $this->log_line("Updated (set to hidden) course {$coursecode} in Moodle (Moodle ID is {$courseid})");
                     }
                 }
             }
         }
     }
 }