Example #1
0
 /**
  * Assign a class to a track, this function also creates
  * and assigns the class to the curriculum default track
  *
  * @return TODO: add meaningful return value
  */
 function add()
 {
     global $CURMAN;
     if (empty($this->courseid)) {
         $this->courseid = $CURMAN->db->get_field(CLSTABLE, 'courseid', 'id', $this->classid);
     }
     if ((empty($this->trackid) or empty($this->classid) or empty($this->courseid)) and empty($CURMAN->config->userdefinedtrack)) {
         cm_error('trackid and classid have not been properly initialized');
         return false;
     } elseif ((empty($this->courseid) or empty($this->classid)) and $CURMAN->config->userdefinedtrack) {
         cm_error('courseid has not been properly initialized');
     }
     if (empty($CURMAN->config->userdefinedtrack)) {
         if ($this->is_class_assigned_to_track()) {
             return false;
         }
         // Determine whether class is required
         $curcrsobj = new curriculumcourse(array('curriculumid' => $this->track->curid, 'courseid' => $this->classid));
         // insert assignment record
         $this->data_insert_record();
         if ($this->autoenrol && $this->is_autoenrollable()) {
             // autoenrol all users in the track
             $users = usertrack::get_users($this->trackid);
             if (!empty($users)) {
                 foreach ($users as $user) {
                     $stu_record = new object();
                     $stu_record->userid = $user->userid;
                     $stu_record->user_idnumber = $user->idnumber;
                     $stu_record->classid = $this->classid;
                     $stu_record->enrolmenttime = time();
                     $enrolment = new student($stu_record);
                     // check prerequisites and enrolment limits
                     $enrolment->add(array('prereq' => 1, 'waitlist' => 1));
                 }
             }
         }
     } else {
         // Look for all the curricula course is linked to -
         // then pull up the default system track for each curricula -
         // and add class to each default system track
         $currculums = curriculumcourse_get_list_by_course($this->courseid);
         $currculums = is_array($currculums) ? $currculums : array();
         foreach ($currculums as $recid => $record) {
             // Create default track for curriculum
             $trkojb = new track(array('curid' => $record->curriculumid));
             $trkid = $trkojb->create_default_track();
             // Create track assignment object
             $trkassign = new trackassignmentclass(array('trackid' => $trkid, 'classid' => $this->classid, 'courseid' => $this->courseid));
             // Check if class is already assigned to default track
             if (!$trkassign->is_class_assigned_to_default_track()) {
                 // Determine whether class is required
                 $curcrsobj = new curriculumcourse(array('curriculumid' => $trkassign->track->curid, 'courseid' => $trkassign->courseid));
                 // Get required field and determine if class is autoenrol eligible
                 $trkassign->autoenrol = (1 == $trkassign->cmclass->count_course_assignments($trkassign->cmclass->courseid) and true === $curcrsobj->is_course_required()) ? 1 : 0;
                 // assign class to the curriculum's default track
                 $trkassign->assign_class_to_default_track();
             }
         }
     }
     events_trigger('crlm_track_class_associated', $this);
     return true;
 }