Exemplo n.º 1
0
 /**
  * Process each course individually.
  * @return void
  */
 protected function process_courses()
 {
     global $DB;
     $this->progress->start_progress(get_string('migratingcourses', 'tool_lpmigrate'), count($this->coursescompetencies));
     // Process each course.
     foreach ($this->coursescompetencies as $courseid => $competencyids) {
         $this->progress->increment_progress();
         $competenciestoremovefromcourse = array();
         $skipcompetencies = array();
         // First, add all the new competencies to the course.
         foreach ($competencyids as $key => $competencyid) {
             $this->coursecompetencyexpectedmigrations++;
             $mapto = isset($this->mappings[$competencyid]) ? $this->mappings[$competencyid] : false;
             // Skip the competencies that are not mapped.
             if ($mapto === false) {
                 $this->missingmappings[$competencyid] = true;
                 if ($this->removewhenmappingismissing) {
                     $competenciestoremovefromcourse[$competencyid] = true;
                 }
                 continue;
             }
             $transaction = $DB->start_delegated_transaction();
             try {
                 // Add the new competency to the course.
                 if (api::add_competency_to_course($courseid, $mapto)) {
                     // Find the added course competency.
                     $cc = course_competency::get_record(array('courseid' => $courseid, 'competencyid' => $mapto));
                     // Set the rule.
                     api::set_course_competency_ruleoutcome($cc, $this->coursescompetenciesoutcomes[$courseid][$competencyid]);
                     // Adapt the sortorder.
                     api::reorder_course_competency($courseid, $mapto, $competencyid);
                     $competenciestoremovefromcourse[$competencyid] = true;
                     $this->coursecompetencymigrations++;
                 } else {
                     // The competency was already in the course...
                     if ($this->removeoriginalwhenalreadypresent) {
                         $competenciestoremovefromcourse[$competencyid] = true;
                     } else {
                         $this->log_warning($courseid, $competencyid, null, get_string('warningdestinationcoursecompetencyalreadyexists', 'tool_lpmigrate'));
                     }
                 }
             } catch (moodle_exception $e) {
                 // There was a major problem with this competency, we will ignore it entirely for the course.
                 $skipcompetencies[$competencyid] = true;
                 $this->log_error($courseid, $competencyid, null, get_string('errorwhilemigratingcoursecompetencywithexception', 'tool_lpmigrate', $e->getMessage()));
                 try {
                     $transaction->rollback($e);
                 } catch (moodle_exception $e) {
                     // Catch the re-thrown exception.
                 }
                 continue;
             }
             $transaction->allow_commit();
         }
         // Then, convert the module competencies.
         if (!empty($this->modulecompetencies[$courseid])) {
             foreach ($this->modulecompetencies[$courseid] as $cmid => $competencyids) {
                 foreach ($competencyids as $competencyid) {
                     $this->modulecompetencyexpectedmigrations++;
                     // This mapped competency was not added to the course.
                     if (!empty($skipcompetencies[$competencyid])) {
                         continue;
                     }
                     $remove = true;
                     $mapto = isset($this->mappings[$competencyid]) ? $this->mappings[$competencyid] : false;
                     // We don't have mapping.
                     if ($mapto === false) {
                         if (!$this->removewhenmappingismissing) {
                             $remove = false;
                         }
                     } else {
                         // We have a mapping.
                         $transaction = $DB->start_delegated_transaction();
                         try {
                             // The competency was added successfully.
                             if (api::add_competency_to_course_module($cmid, $mapto)) {
                                 // Find the added module competency.
                                 $mc = course_module_competency::get_record(array('cmid' => $cmid, 'competencyid' => $mapto));
                                 // Set the competency rule.
                                 api::set_course_module_competency_ruleoutcome($mc, $this->modulecompetenciesoutcomes[$courseid][$cmid][$competencyid]);
                                 // Adapt the sortorder.
                                 api::reorder_course_module_competency($cmid, $mapto, $competencyid);
                                 $this->modulecompetencymigrations++;
                             } else {
                                 // The competency was already in the module.
                                 if (!$this->removeoriginalwhenalreadypresent) {
                                     $remove = false;
                                     $competencieswithissues[$competencyid] = true;
                                     $this->log_warning($courseid, $competencyid, $cmid, get_string('warningdestinationmodulecompetencyalreadyexists', 'tool_lpmigrate'));
                                 }
                             }
                         } catch (moodle_exception $e) {
                             // There was a major problem with this competency in this module.
                             $competencieswithissues[$competencyid] = true;
                             $message = get_string('errorwhilemigratingmodulecompetencywithexception', 'tool_lpmigrate', $e->getMessage());
                             $this->log_error($courseid, $competencyid, $cmid, $message);
                             try {
                                 $transaction->rollback($e);
                             } catch (moodle_exception $e) {
                                 // Catch the re-thrown exception.
                             }
                             continue;
                         }
                         $transaction->allow_commit();
                     }
                     try {
                         // Go away competency!
                         if ($remove && api::remove_competency_from_course_module($cmid, $competencyid)) {
                             $this->modulecompetencyremovals++;
                         }
                     } catch (moodle_exception $e) {
                         $competencieswithissues[$competencyid] = true;
                         $this->log_warning($courseid, $competencyid, $cmid, get_string('warningcouldnotremovemodulecompetency', 'tool_lpmigrate'));
                     }
                 }
             }
         }
         // Finally, we remove the course competencies, but only for the 100% successful ones.
         foreach ($competenciestoremovefromcourse as $competencyid => $unused) {
             // Skip competencies with issues.
             if (isset($competencieswithissues[$competencyid])) {
                 continue;
             }
             try {
                 // Process the course competency.
                 api::remove_competency_from_course($courseid, $competencyid);
                 $this->coursecompetencyremovals++;
             } catch (moodle_exception $e) {
                 $this->log_warning($courseid, $competencyid, null, get_string('warningcouldnotremovecoursecompetency', 'tool_lpmigrate'));
             }
         }
     }
     $this->progress->end_progress();
 }
Exemplo n.º 2
0
 /**
  * Remove a competency from this course module.
  *
  * @param mixed $cmorid The course module, or id of the course module
  * @param int $competencyid The id of the competency
  * @return bool
  */
 public static function remove_competency_from_course_module($cmorid, $competencyid)
 {
     static::require_enabled();
     $cm = $cmorid;
     if (!is_object($cmorid)) {
         $cm = get_coursemodule_from_id('', $cmorid, 0, true, MUST_EXIST);
     }
     // Check the user have access to the course module.
     self::validate_course_module($cm);
     // First we do a permissions check.
     $context = context_module::instance($cm->id);
     require_capability('moodle/competency:coursecompetencymanage', $context);
     $record = new stdClass();
     $record->cmid = $cm->id;
     $record->competencyid = $competencyid;
     $competency = new competency($competencyid);
     $exists = course_module_competency::get_record(array('cmid' => $cm->id, 'competencyid' => $competencyid));
     if ($exists) {
         return $exists->delete();
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  * Assert that the course module competency was migrated.
  *
  * @param stdClass $cm The CM.
  * @param competency $compfrom The competency from.
  * @param competency $compto The competency to.
  */
 protected function assertModuleCompetencyMigrated($cm, $compfrom, $compto)
 {
     $cmcs = $this->cmcs[$cm->cmid];
     $this->assertModuleCompetencyNotExists($cm, $compfrom);
     $this->assertModuleCompetencyExists($cm, $compto);
     $before = $cmcs[$compfrom->get_id()];
     $after = course_module_competency::get_record(array('cmid' => $cm->cmid, 'competencyid' => $compto->get_id()));
     $this->assertNotEquals($before->get_id(), $after->get_id());
     $this->assertEquals($before->get_cmid(), $after->get_cmid());
     $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
     $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
 }