Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 184... Línea 184...
184
                    if ($instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'database'), '*', IGNORE_MULTIPLE)) {
184
                    if ($instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'database'), '*', IGNORE_MULTIPLE)) {
185
                        $instances[$course->id] = $instance;
185
                        $instances[$course->id] = $instance;
186
                        continue;
186
                        continue;
187
                    }
187
                    }
Línea -... Línea 188...
-
 
188
 
-
 
189
                    $timeout = 5;
-
 
190
                    $locktype = 'enrol_database_user_enrolments';
-
 
191
                    $resource = 'course:' . $course->id;
-
 
192
                    $lockfactory = \core\lock\lock_config::get_lock_factory($locktype);
-
 
193
                    if ($lock = $lockfactory->get_lock($resource, $timeout)) {
-
 
194
                        try {
-
 
195
                            $instance = $DB->get_record('enrol', ['enrol' => 'database', 'courseid' => $course->id]);
188
 
196
                            if (!$instance) {
189
                    $enrolid = $this->add_instance($course);
197
                                $enrolid = $this->add_instance($course);
-
 
198
                                $instance = $DB->get_record('enrol', ['id' => $enrolid]);
-
 
199
                            }
-
 
200
                        } finally {
-
 
201
                            $lock->release();
-
 
202
                        }
-
 
203
                    } else {
-
 
204
                        // Attempt to reuse an existing record added by another process during race condition.
-
 
205
                        if ($instance = $DB->get_record('enrol', ['enrol' => 'database', 'courseid' => $course->id])) {
-
 
206
                            $instances[$course->id] = $instance;
-
 
207
                            continue;
-
 
208
                        } else {
-
 
209
                            // Give up.
-
 
210
                            throw new moodle_exception(
-
 
211
                                'locktimeout',
-
 
212
                                'enrol_database',
-
 
213
                                '',
-
 
214
                                null,
-
 
215
                                'Could not create database enrolment instance for course ' . $course->id
-
 
216
                            );
-
 
217
                        }
-
 
218
                    }
190
                    $instances[$course->id] = $DB->get_record('enrol', array('id'=>$enrolid));
219
                    $instances[$course->id] = $instance;
191
                }
220
                }
192
            }
221
            }
193
            $rs->Close();
222
            $rs->Close();
194
            $extdb->Close();
223
            $extdb->Close();
Línea 667... Línea 696...
667
            $trace->output('Error while communicating with external enrolment database');
696
            $trace->output('Error while communicating with external enrolment database');
668
            $trace->finished();
697
            $trace->finished();
669
            return 1;
698
            return 1;
670
        }
699
        }
Línea -... Línea 700...
-
 
700
 
-
 
701
        $courseconfig = get_config('moodlecourse');
671
 
702
 
672
        $table     = $this->get_config('newcoursetable');
703
        $table     = $this->get_config('newcoursetable');
673
        $fullname  = trim($this->get_config('newcoursefullname'));
704
        $fullname  = trim($this->get_config('newcoursefullname'));
674
        $shortname = trim($this->get_config('newcourseshortname'));
705
        $shortname = trim($this->get_config('newcourseshortname'));
675
        $idnumber  = trim($this->get_config('newcourseidnumber'));
706
        $idnumber  = trim($this->get_config('newcourseidnumber'));
Línea -... Línea 707...
-
 
707
        $category  = trim($this->get_config('newcoursecategory'));
-
 
708
 
-
 
709
        $startdate = trim($this->get_config('newcoursestartdate'));
676
        $category  = trim($this->get_config('newcoursecategory'));
710
        $enddate   = trim($this->get_config('newcourseenddate'));
677
 
711
 
678
        // Lowercased versions - necessary because we normalise the resultset with array_change_key_case().
712
        // Lowercased versions - necessary because we normalise the resultset with array_change_key_case().
679
        $fullname_l  = strtolower($fullname);
713
        $fullname_l  = strtolower($fullname);
680
        $shortname_l = strtolower($shortname);
714
        $shortname_l = strtolower($shortname);
-
 
715
        $idnumber_l  = strtolower($idnumber);
-
 
716
        $category_l  = strtolower($category);
Línea 681... Línea 717...
681
        $idnumber_l  = strtolower($idnumber);
717
        $startdatelowercased = strtolower($startdate);
682
        $category_l  = strtolower($category);
718
        $enddatelowercased   = strtolower($enddate);
Línea 683... Línea 719...
683
 
719
 
Línea 696... Línea 732...
696
            $sqlfields[] = $category;
732
            $sqlfields[] = $category;
697
        }
733
        }
698
        if ($idnumber) {
734
        if ($idnumber) {
699
            $sqlfields[] = $idnumber;
735
            $sqlfields[] = $idnumber;
700
        }
736
        }
-
 
737
        if ($startdate) {
-
 
738
            $sqlfields[] = $startdate;
-
 
739
        }
-
 
740
        if ($enddate) {
-
 
741
            $sqlfields[] = $enddate;
-
 
742
        }
-
 
743
 
701
        $sql = $this->db_get_sql($table, array(), $sqlfields, true);
744
        $sql = $this->db_get_sql($table, array(), $sqlfields, true);
702
        $createcourses = array();
745
        $createcourses = array();
703
        if ($rs = $extdb->Execute($sql)) {
746
        if ($rs = $extdb->Execute($sql)) {
704
            if (!$rs->EOF) {
747
            if (!$rs->EOF) {
705
                while ($fields = $rs->FetchRow()) {
748
                while ($fields = $rs->FetchRow()) {
Línea 720... Línea 763...
720
                    }
763
                    }
721
                    $course = new stdClass();
764
                    $course = new stdClass();
722
                    $course->fullname  = $fields[$fullname_l];
765
                    $course->fullname  = $fields[$fullname_l];
723
                    $course->shortname = $fields[$shortname_l];
766
                    $course->shortname = $fields[$shortname_l];
724
                    $course->idnumber  = $idnumber ? $fields[$idnumber_l] : '';
767
                    $course->idnumber  = $idnumber ? $fields[$idnumber_l] : '';
-
 
768
 
725
                    if ($category) {
769
                    if ($category) {
726
                        if (empty($fields[$category_l])) {
770
                        if (empty($fields[$category_l])) {
727
                            // Empty category means use default.
771
                            // Empty category means use default.
728
                            $course->category = $defaultcategory;
772
                            $course->category = $defaultcategory;
729
                        } else if ($coursecategory = $DB->get_record('course_categories', array($localcategoryfield=>$fields[$category_l]), 'id')) {
773
                        } else if ($coursecategory = $DB->get_record('course_categories', array($localcategoryfield=>$fields[$category_l]), 'id')) {
Línea 736... Línea 780...
736
                            continue;
780
                            continue;
737
                        }
781
                        }
738
                    } else {
782
                    } else {
739
                        $course->category = $defaultcategory;
783
                        $course->category = $defaultcategory;
740
                    }
784
                    }
-
 
785
 
-
 
786
                    if ($startdate) {
-
 
787
                        if (!empty($fields[$startdatelowercased])) {
-
 
788
                            $course->startdate = is_number($fields[$startdatelowercased])
-
 
789
                                ? $fields[$startdatelowercased]
-
 
790
                                : strtotime($fields[$startdatelowercased]);
-
 
791
 
-
 
792
                            // Broken start date. Stop syncing this course.
-
 
793
                            if ($course->startdate === false) {
-
 
794
                                $trace->output('error: invalid external course start date value: ' . json_encode($fields), 1);
-
 
795
                                continue;
-
 
796
                            }
-
 
797
                        }
-
 
798
                    }
-
 
799
 
-
 
800
                    if ($enddate) {
-
 
801
                        if (!empty($fields[$enddatelowercased])) {
-
 
802
                            $course->enddate = is_number($fields[$enddatelowercased])
-
 
803
                                ? $fields[$enddatelowercased]
-
 
804
                                : strtotime($fields[$enddatelowercased]);
-
 
805
 
-
 
806
                            // Broken end date. Stop syncing this course.
-
 
807
                            if ($course->enddate === false) {
-
 
808
                                $trace->output('error: invalid external course end date value: ' . json_encode($fields), 1);
-
 
809
                                continue;
-
 
810
                            }
-
 
811
                        }
-
 
812
                    }
-
 
813
 
741
                    $createcourses[] = $course;
814
                    $createcourses[] = $course;
742
                }
815
                }
743
            }
816
            }
744
            $rs->Close();
817
            $rs->Close();
745
        } else {
818
        } else {
Línea 767... Línea 840...
767
                } else {
840
                } else {
768
                    $trace->output("can not find template for new course!", 1);
841
                    $trace->output("can not find template for new course!", 1);
769
                }
842
                }
770
            }
843
            }
771
            if (!$template) {
844
            if (!$template) {
772
                $courseconfig = get_config('moodlecourse');
-
 
773
                $template = new stdClass();
845
                $template = new stdClass();
774
                $template->summary        = '';
846
                $template->summary        = '';
775
                $template->summaryformat  = FORMAT_HTML;
847
                $template->summaryformat  = FORMAT_HTML;
776
                $template->format         = $courseconfig->format;
848
                $template->format         = $courseconfig->format;
777
                $template->numsections    = $courseconfig->numsections;
849
                $template->numsections    = $courseconfig->numsections;
Línea 796... Línea 868...
796
                $newcourse->fullname  = $fields->fullname;
868
                $newcourse->fullname  = $fields->fullname;
797
                $newcourse->shortname = $fields->shortname;
869
                $newcourse->shortname = $fields->shortname;
798
                $newcourse->idnumber  = $fields->idnumber;
870
                $newcourse->idnumber  = $fields->idnumber;
799
                $newcourse->category  = $fields->category;
871
                $newcourse->category  = $fields->category;
Línea -... Línea 872...
-
 
872
 
-
 
873
                if (isset($fields->startdate)) {
-
 
874
                    $newcourse->startdate = $fields->startdate;
-
 
875
                }
-
 
876
 
-
 
877
                if (isset($fields->enddate)) {
-
 
878
                    // Validating end date.
-
 
879
                    if ($fields->enddate > 0 && $newcourse->startdate > $fields->enddate) {
-
 
880
                        $trace->output(
-
 
881
                            "can not insert new course, the end date must be after the start date: " . $newcourse->shortname, 1
-
 
882
                        );
-
 
883
                        continue;
-
 
884
                    }
-
 
885
                    $newcourse->enddate = $fields->enddate;
-
 
886
                } else {
-
 
887
                    if ($courseconfig->courseenddateenabled) {
-
 
888
                        $newcourse->enddate = $newcourse->startdate + $courseconfig->courseduration;
-
 
889
                    }
-
 
890
                }
800
 
891
 
801
                // Detect duplicate data once again, above we can not find duplicates
892
                // Detect duplicate data once again, above we can not find duplicates
802
                // in external data using DB collation rules...
893
                // in external data using DB collation rules...
803
                if ($DB->record_exists('course', array('shortname' => $newcourse->shortname))) {
894
                if ($DB->record_exists('course', array('shortname' => $newcourse->shortname))) {
804
                    $trace->output("can not insert new course, duplicate shortname detected: ".$newcourse->shortname, 1);
895
                    $trace->output("can not insert new course, duplicate shortname detected: ".$newcourse->shortname, 1);