/**
  * Performs the data importation from Moodle core tables (courses, enrolled users, logs). This importation is done only if
  * database_helper->has_data_to_be_imported() returns true.
  *
  * @param int $courseid The current course id for which the data has to be imported.
  */
 protected function import_data($courseid)
 {
     $courseyear = $this->db->get_course_start_week_and_year($courseid)['year'];
     $previouscourses = $this->db->find_course_previous_teaching_ids_core_tables($courseid, $courseyear);
     foreach ($previouscourses as $previouscourse) {
         $this->db->dump_previous_core_info_to_historic_tables($previouscourse);
     }
     $previouscourses = $this->db->find_course_previous_teachings_ids_historic_tables($courseid, $courseyear);
     $this->db->insert_courses_associations($courseid, $previouscourses);
 }
 /**
  * Performs all the operations in order to display the block output:
  *  - Checks if it is the first time that the block is loaded in the course, to check if the course is personalizable
  *    or not.
  *  - Checks if the current user is selected to receive the recommendations.
  *  - Retrieves the recommendations from the database.
  *
  * @return string The content of the block.
  */
 public function get_content()
 {
     global $COURSE, $USER, $DB;
     if ($this->content !== null) {
         return $this->content;
     }
     if (empty($this->instance)) {
         $this->content = '';
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->text = '';
     $context = context_course::instance($COURSE->id);
     if (has_capability('block/mycourse_recommendations:importfromcsv', $context)) {
         $this->content->footer = $this->generate_footer_import_url($COURSE->id);
     } else {
         $this->content->footer = '';
     }
     $courseyear = $this->db->get_course_start_week_and_year($COURSE->id)['year'];
     $firstinstance = $this->db->is_blocks_first_instance($COURSE->id);
     if ($firstinstance) {
         $this->initialize_course($COURSE->id, $courseyear);
     }
     $isuser = has_capability('block/mycourse_recommendations:recommendationstext', $context);
     if ($isuser) {
         $personalizable = $this->db->is_course_personalizable($COURSE->id);
         if ($personalizable) {
             $active = $this->db->is_course_active($COURSE->id);
             if ($active) {
                 $userselected = $this->db->is_user_selected_for_course($USER->id, $COURSE->id);
                 if (!$userselected) {
                     $this->content->text = get_string('usernotselected', 'block_mycourse_recommendations');
                 } else {
                     $currentweek = $this->get_current_week();
                     $recommendations = $this->db->get_recommendations($COURSE->id, $USER->id, $currentweek);
                     $this->content->text = recommendations_renderer::render_recommendations($recommendations);
                 }
             } else {
                 $this->content->text = get_string('inactive', 'block_mycourse_recommendations');
             }
         } else {
             $this->content->text = get_string('notpersonalizable', 'block_mycourse_recommendations');
         }
     } else {
         $this->content->text = get_string('notastudent', 'block_mycourse_recommendations');
     }
     return $this->content;
 }
 /**
  * Determines if a course is personalizable or not. The logic to determine this is divided in other
  * functions; this function only calls them. If one of these function says that doesn't meet one of
  * the parameters, there's no need to continue calling other functions.
  *
  * @see meets_minimum_weeks($courseid, $db).
  * @see meets_minimum_previous_courses($courseid, $db).
  * @see meets_minimum_resources($courseid, $db).
  * @see meets_minimum_previous_students($courseid, $db).
  * @see meets_resource_variation($courseid, $db).
  * @param int $courseid The course to determine if it is personalizable or not.
  * @param int $year The year the course teaching began.
  * @return boolean If the given course is personalizable or not.
  */
 public static function is_course_personalizable($courseid, $year)
 {
     $db = new database_helper();
     $personalizable = true;
     $coursestartyear = $db->get_course_start_week_and_year($courseid)['year'];
     $personalizable &= self::meets_minimum_previous_courses($courseid, $coursestartyear, $db);
     if ($personalizable) {
         $personalizable &= self::meets_minimum_weeks($courseid, $db);
     }
     if ($personalizable) {
         $personalizable &= self::meets_minimum_resources($courseid, $coursestartyear, $db);
     }
     if ($personalizable) {
         $personalizable &= self::meets_minimum_previous_students($courseid, $coursestartyear, $db);
     }
     if ($personalizable) {
         $personalizable &= self::meets_resource_variation($courseid, $db);
     }
     return $personalizable;
 }