Example #1
0
 /**
  * Update a course
  * @todo: consider factoring this some more once other actions exist
  *
  * @param object $record One record of import data
  * @param string $filename The import file name, used for logging
  * @return boolean true on success, otherwise false
  */
 function course_update($record, $filename)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php';
     $message = "";
     //field length checking
     $lengthcheck = $this->check_course_field_lengths($record, $filename);
     if (!$lengthcheck) {
         return false;
     }
     if (isset($record->idnumber)) {
         if (!($crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $record->idnumber)))) {
             $identifier = $this->get_field_mapping('idnumber');
             $this->fslogger->log_failure("{$identifier} value of \"{$record->idnumber}\" does not refer to a valid course description.", 0, $filename, $this->linenumber, $record, "course");
             return false;
         }
     }
     $currid = 0;
     if (isset($record->assignment)) {
         if (!($currid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $record->assignment)))) {
             $identifier = $this->get_field_mapping('assignment');
             $this->fslogger->log_failure("{$identifier} value of \"{$record->assignment}\" does not refer to a valid program.", 0, $filename, $this->linenumber, $record, "course");
             return false;
         } else {
             if ($DB->record_exists(curriculumcourse::TABLE, array('curriculumid' => $currid, 'courseid' => $crsid))) {
                 $identifier = $this->get_field_mapping('idnumber');
                 $message = "Course description with {$identifier} \"{$record->idnumber}\" already assigned to program with idnumber \"{$record->assignment}\".";
             }
         }
     }
     if (!$this->validate_course_data('update', $record, $filename)) {
         return false;
     }
     $record = $this->add_custom_field_prefixes($record);
     //custom field validation
     if (!$this->validate_custom_field_data('update', $record, $filename, 'course')) {
         return false;
     }
     $record = $this->initialize_course_fields($record);
     $record->id = $crsid;
     $course = new course();
     $course->set_from_data($record);
     $course->save();
     if ($currid != 0) {
         $currcrs = new curriculumcourse();
         $assoc = new stdClass();
         $assoc->curriculumid = $currid;
         $assoc->courseid = $course->id;
         $currcrs->set_from_data($assoc);
         $currcrs->save();
     }
     //associate this course description to a Moodle course, if necessary
     $this->associate_course_to_moodle_course($record, $course->id);
     //log success
     $success_message = "Course description with idnumber \"{$record->idnumber}\" successfully updated.";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }